mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
1 line
1.8 KiB
JavaScript
1 line
1.8 KiB
JavaScript
(()=>{return(t,e)=>{class n{}class r extends n{constructor(){super(...arguments);this.name="Bing";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 s extends n{constructor(){super(...arguments);this.name="Google";this.link="https://translate.google.com/";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 o extends s{constructor(){super(...arguments);this.link="https://translate.google.cn/";this.apiUrl="https://translate.google.cn/translate_a/single"}}const a=()=>{switch(t.feedsTranslateProvider){default:case"Bing":return new r;case"Google":return new s;case"GoogleCN":return new o}};return{export:{getTranslator:a}}}})(); |