您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
选中英文文字并使用 OpenAI GPT 模型翻译为中文
当前为
// ==UserScript== // @namespace 英文翻译 // @version 1.0 // @description 选中英文文字并使用 OpenAI GPT 模型翻译为中文 // @match *://*/* // @license MIT // @name 英文翻译 // ==/UserScript== (function() { // 监听鼠标选中事件 document.addEventListener("mouseup", function(event) { const selectedText = window.getSelection().toString(); // 检查选中的文字是否为英文 if (selectedText) { // 提示用户是否需要翻译 if (confirm("是否需要翻译此文本?")) { // 调用 OpenAI GPT 模型进行翻译 const apiKey = "你自己的 apikey"; const url = "https://api.openai.com/v1/engines/text-davinci-002/completions"; const headers = { "Content-Type": "application/json", "Authorization": `Bearer ${apiKey}` }; const data = { "prompt": `Translate the following English text into Chinese:\n\n${selectedText}`, "temperature": 0.7, "max_tokens": 1000, "top_p": 1, "frequency_penalty": 0, "presence_penalty": 0 }; fetch(url, { method: "POST", headers: headers, body: JSON.stringify(data) }) .then(response => response.json()) .then(result => { // 创建翻译结果和原文的 DOM 元素 const translationElement = document.createElement("span"); translationElement.style.backgroundColor = "#ffff00"; translationElement.style.color = "#000000"; translationElement.style.fontWeight = "bold"; translationElement.style.borderRadius = "3px"; translationElement.style.padding = "2px"; translationElement.style.marginLeft = "2px"; translationElement.appendChild(document.createTextNode(result.choices[0].text)); const originalElement = document.createElement("span"); originalElement.style.backgroundColor = "#ffffff"; originalElement.style.color = "#000000"; originalElement.style.borderRadius = "3px"; originalElement.style.padding = "2px"; originalElement.appendChild(document.createTextNode(selectedText)); // 替换选中的文字为翻译结果和原文 const range = window.getSelection().getRangeAt(0); range.deleteContents(); range.insertNode(translationElement); range.insertNode(originalElement); }) .catch(error => { console.error(error); }); } } }); })();