您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Automatically translate selected text to English using LibreTranslate API.
// ==UserScript== // @name Auto Translate to English (LibreTranslate) // @namespace http://tampermonkey.net/ // @version 0.1 // @description Automatically translate selected text to English using LibreTranslate API. // @author Da cat // @match *://*/* // @grant GM_setClipboard // ==/UserScript== (function() { 'use strict'; const LIBRETRANSLATE_API_URL = 'https://libretranslate.com/translate'; // LibreTranslate API endpoint // Function to translate the selected text function translateText(text) { const data = { q: text, source: 'auto', // Auto-detect language target: 'en', // Translate to English format: 'text' }; const xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.addEventListener('readystatechange', function () { if (this.readyState === this.DONE) { const response = JSON.parse(this.responseText); console.log('Translation Response:', response); // Check if the response has a translation if (response && response.translatedText) { const translatedText = response.translatedText; alert(`Translated Text: ${translatedText}`); // Optionally, copy the translated text to the clipboard GM_setClipboard(translatedText); console.log('Translated text copied to clipboard'); } else { alert('Translation failed. Please check the console for details.'); } } }); xhr.open('POST', LIBRETRANSLATE_API_URL); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.send(JSON.stringify(data)); } // Function to handle context menu click function handleContextMenu(event) { const selectedText = window.getSelection().toString().trim(); if (selectedText) { translateText(selectedText); } else { alert('Please select some text to translate.'); } } // Add context menu command document.addEventListener('contextmenu', function(event) { const selectedText = window.getSelection().toString().trim(); if (selectedText) { event.preventDefault(); // Prevent default context menu handleContextMenu(event); // Call the translate function } }); })();