上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖
楼主: Saltboar - 

求助:关于Popup Search脚本自定义翻译引擎无效的问题

[复制链接]
  • TA的每日心情
    无聊
    2024-8-17 23:01
  • 签到天数: 2 天

    [LV.1]初来乍到

    1

    主题

    0

    回帖

    5

    积分

    助理工程师

    积分
    5
    发表于 2024-4-23 12:22:58 | 显示全部楼层 | 阅读模式
    悬赏8油猫币未解决

    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
        });
      }

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表