QuantumultX 实时翻译脚本:DeepL API 使用教程
{"title":"QuantumultX 实时翻译脚本:DeepL API 使用教程","description":"使用QuantumultX和DeepL API创建实时翻译脚本,让您在浏览网页时自动将文本翻译成中文。","keywords":"QuantumultX, 实时翻译, DeepL API, 脚本, 教程","content":""[rewrite_local]\n# 实时翻译脚本\n^https://api.deepl.com/v2/translate url script-response-body deepl_translate.js\n\n[mitm]\nhostname = api.deepl.com"\n\n"在这个示例中,我们使用了DeepL翻译API来进行实时翻译。首先,我们使用rewrite_local将与DeepL翻译API的URL匹配的请求重定向到我们的脚本文件deepl_translate.js。\n\n接下来,我们需要创建一个脚本文件deepl_translate.js,该文件使用JavaScript编写,用于处理请求和响应,并进行翻译操作。示例代码如下:\n\njavascript\nconst apiKey = 'YOUR_DEEPL_API_KEY'; // 替换为你的DeepL API密钥\n\nconst sourceLang = 'auto'; // 源语言,auto表示自动检测\nconst targetLang = 'zh'; // 目标语言,这里是中文\n\nconst url = $request.url;\nconst body = JSON.parse($response.body);\n\nconst text = body.text; // 获取需要翻译的文本\n\n// 创建用于发送翻译请求的URL\nconst translateUrl = `https:\/\/api.deepl.com\/v2\/translate?auth_key=${apiKey}&text=${encodeURIComponent(text)}&source_lang=${sourceLang}&target_lang=${targetLang}`;\n\n$httpClient.get(translateUrl, (error, response, data) => {\n if (error) {\n $done({});\n } else {\n const translatedText = JSON.parse(data).translations[0].text;\n\n // 修改响应数据,替换为翻译后的文本\n body.translation = translatedText;\n\n $done({ body: JSON.stringify(body) });\n }\n});\n\n\n请注意,你需要将YOUR_DEEPL_API_KEY替换为你的DeepL API密钥。你可以在DeepL网站上注册一个账号并获取API密钥。\n\n完成以上步骤后,将QuantumultX配置文件中的脚本部分更新为上述示例代码,并重新加载配置文件。然后,当你使用QuantumultX访问支持翻译的网站时,它将自动将文本翻译为中文并返回给你。"}
原文地址: https://www.cveoy.top/t/topic/qfFj 著作权归作者所有。请勿转载和采集!