MCPcopy Create free account
hub / github.com/Bistutu/FluentRead / deepL

Function deepL

userscripts.js:1102–1141  ·  view source on GitHub ↗
(origin, native = false)

Source from the content-addressed store, hash-verified

1100// region deepL
1101// native 判断是否为本地部署模型(native 不支持 html,暂保留)
1102function deepL(origin, native = false) {
1103 return new Promise((resolve, reject) => {
1104 let target_lang = langManager.getTo();
1105 if (target_lang === 'zh-Hans') target_lang = 'zh'; // DeepL 不支持 zh-Hans
1106
1107 // 获取 DeepL API 密钥
1108 let deepLAuthKey = tokenManager.getToken(transModel.deepL);
1109
1110 // 判断是否为本地部署模型(模型所需参数些许不同)
1111 let text = native ? origin : [origin]
1112 let url = native ? "http://127.0.0.1:1188/translate" : "https://api-free.deepl.com/v2/translate"
1113
1114 // 发起翻译请求
1115 GM_xmlhttpRequest({
1116 method: 'POST',
1117 url: url,
1118 headers: {
1119 'Content-Type': 'application/json',
1120 'Authorization': 'DeepL-Auth-Key ' + deepLAuthKey
1121 },
1122 data: JSON.stringify({
1123 text: text,
1124 target_lang: target_lang,
1125 tag_handling: 'html',
1126 context: url.host + document.title, // 添加上下文辅助
1127 preserve_formatting: true
1128 }),
1129 onload: resp => {
1130 try {
1131 let resultJson = JSON.parse(resp.responseText);
1132 if (native) resolve(resultJson.data);
1133 else resolve(resultJson.translations[0].text);
1134 } catch (e) {
1135 reject(resp.responseText);
1136 }
1137 },
1138 onerror: error => reject(error)
1139 });
1140 });
1141}
1142
1143// endregion
1144

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected