Popup Search脚本是一款划词弹窗便捷搜索的脚本,由于作者已停更,脚本中的Deepl翻译引擎失效,所以我借助AI大模型,根据Deeplx的调用示例修改了相关内容,但实测依然无法连接,求大神解惑。
注:API地址在其他脚本中可用。
原代码节选
onTranslate = function (engine) {
popData.bTrans = 1;
$("#transPanel").empty().append(`<div style='padding:10px;'><img src='${popData.icons.pending}' /></div>`).show();
$('#popupWrapper').hide();
fixPos(document.defaultView.getSelection());
return doRequest(engine, 0, 2000);
};
doRequest = function (engine, i, wait) {
var ErrHandle, lang;
ErrHandle = function () {
return doRequest(engine, i + 1, wait + 2000);
};
if (i >= 2) {
ErrHandle = ajaxError;
}
lang = navigator.language || navigator.userLanguage || "zh-CN";
if (engine === 'google') {
return GM_xmlhttpRequest({
method: 'POST',
url: `https://translate.google.com/translate_a/single?client=gtx&dj=1&q=${popData.text}&sl=auto&tl=${lang}&hl=${lang}&ie=UTF-8&oe=UTF-8&source=icon&dt=t&dt=bd`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
"Accept-Encoding": "gzip, deflate"
},
timeout: wait,
onload: parseTranslationGoogle,
onerror: ErrHandle,
ontimeout: ErrHandle
});
} else if (engine === 'deepl') {
return GM_xmlhttpRequest({
method: 'POST',
url: "https://api-free.deepl.com/v2/translate??auth_key=aa09f88d-ab75-3488-b8a3-18ad27a35870:fx",
data: "auth_key=aa09f88d-ab75-3488-b8a3-18ad27a35870:fx&target_lang=ZH&text=" + popData.text,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
timeout: wait,
onload: parseTranslationDeepl,
onerror: ErrHandle,
ontimeout: ErrHandle
});
} else {
return GM_xmlhttpRequest({
method: 'POST',
url: "http://api.interpreter.caiyunai.com/v1/translator",
headers: {
'Content-Type': 'application/json',
"X-Authorization": "token " + "3975l6lr5pcbvidl6jl2"
},
data: JSON.stringify({
"source": [popData.text],
"trans_type": "auto2zh",
"request_id": "demo",
"detect": true
}),
timeout: wait,
onload: parseTranslationCaiyun,
onerror: ErrHandle,
ontimeout: ErrHandle
});
}
};
parseTranslationDeepl = function (responseDetails) {
var RTxt, Result;
if (!popData.bTrans) {
return;
}
try {
RTxt = JSON.parse(responseDetails.responseText);
} catch (error) {
log(JSON.stringify(responseDetails));
return ajaxError(responseDetails);
}
Result = `<div id=\"tranRst\" style=\"font-size:13px;overflow:auto;padding:5px 12px;\"> <div style=\"line-height:200%;font-size:13px;\"> ${RTxt.translations[0].text} </div> </div>`;
$('#transPanel').empty().append(Result).show();
fixPos(document.defaultView.getSelection());
};
Deeplx调用示例
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"text": "Hello, world!",
"source_lang": "auto",
"target_lang": "ZH"
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://api.deeplx.org/translate", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
修改的代码
else if (engine === 'deeplx') {
return GM_xmlhttpRequest({
method: 'POST',
url: "https://api.deeplx.org/translate",
headers: {
'Content-Type': 'application/json'
},
data: JSON.stringify({
"text": [popData.text],
"source_lang": "auto",
"target_lang": "ZH"
}),
timeout: wait,
onload: parseTranslationDeepl,
onerror: ErrHandle,
ontimeout: ErrHandle
});
}