Bilibili-Evolved/min/feeds-translate-providers.min.js
2021-10-26 13:22:15 +08:00

1 line
2.0 KiB
JavaScript

(()=>(t,e)=>{function n(t,e,n){if(e in t){Object.defineProperty(t,e,{value:n,enumerable:true,configurable:true,writable:true})}else{t[e]=n}return t}class r{constructor(){n(this,"name",void 0);n(this,"link",void 0)}}class s extends r{constructor(...t){super(...t);n(this,"name","Bing");n(this,"link","https://translate.bing.com/")}async translate(t,e){const n="zh-Hans";let r=_.get(e,"targetLanguage",n);if(r.includes("-")&&r!==n){r=r.substring(0,r.indexOf("-"))}try{const e=await Ajax.monkey({url:"https://cn.bing.com/ttranslatev3",method:"POST",data:Object.entries({fromLang:"auto-detect",to:r,text:t}).map((([t,e])=>`${t}=${e}`)).join("&"),headers:{"User-Agent":UserAgent,"Content-Type":"application/x-www-form-urlencoded"},responseType:"json"});const[n]=e;if(n.translations){return n.translations.map((t=>t.text)).join("\n")}else{console.error("Translator: Bing",n);throw new Error("调用Bing翻译失败.")}}catch(t){console.error("Translator: Bing",t);throw new Error("调用Bing翻译失败.")}}}class o extends r{constructor(...t){super(...t);n(this,"name","Google");n(this,"link","https://translate.google.com/");n(this,"apiUrl","https://translate.google.com/translate_a/single")}async translate(t,e){const n="zh";let r=_.get(e,"targetLanguage",n);if(r.includes("-")&&r!==n){r=r.substring(0,r.indexOf("-"))}try{const e=await Ajax.monkey({url:`${this.apiUrl}?${Object.entries({client:"gtx",sl:"auto",tl:r,dt:"t",q:encodeURIComponent(t)}).map((([t,e])=>`${t}=${e}`)).join("&")}`,method:"GET",headers:{"User-Agent":UserAgent},responseType:"json"});const n=e[0].map((t=>t[0])).join("");return n}catch(t){console.error("Translator: Google",t);throw new Error("调用Google翻译失败.")}}}class a extends o{constructor(...t){super(...t);n(this,"link","https://translate.google.cn/");n(this,"apiUrl","https://translate.google.cn/translate_a/single")}}const l=()=>{switch(t.feedsTranslateProvider){default:case"Bing":return new s;case"Google":return new o;case"GoogleCN":return new a}};return{export:{getTranslator:l}}})();