// ==UserScript== // @name Fast Translate // @namespace local.fast-immersive-translate // @homepageURL https://github.com/selenahonsinger/Fast-Translate // @downloadURL https://raw.githubusercontent.com/selenahonsinger/Fast-Translate/main/fast-immersive-translate.user.js // @updateURL https://raw.githubusercontent.com/selenahonsinger/Fast-Translate/main/fast-immersive-translate.user.js // @version 0.6.1 // @charset UTF-8 // @description Fast bilingual page translation with batching, lazy loading, cache, and OpenAI/DeepL/Google/Baidu providers. // @author Fast Translate contributors // @icon data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2NCA2NCI+PGRlZnM+PGxpbmVhckdyYWRpZW50IGlkPSJnIiB4MT0iMCIgeTE9IjAiIHgyPSIxIiB5Mj0iMSI+PHN0b3Agb2Zmc2V0PSIwIiBzdG9wLWNvbG9yPSIjZmY4N2I3Ii8+PHN0b3Agb2Zmc2V0PSIxIiBzdG9wLWNvbG9yPSIjZGIzYjdiIi8+PC9saW5lYXJHcmFkaWVudD48L2RlZnM+PHJlY3Qgd2lkdGg9IjY0IiBoZWlnaHQ9IjY0IiByeD0iMTQiIGZpbGw9InVybCgjZykiLz48dGV4dCB4PSIzMiIgeT0iNDUiIGZvbnQtZmFtaWx5PSInTWljcm9zb2Z0IFlhSGVpJyxzeXN0ZW0tdWksc2Fucy1zZXJpZiIgZm9udC1zaXplPSIzNiIgZm9udC13ZWlnaHQ9IjcwMCIgdGV4dC1hbmNob3I9Im1pZGRsZSIgZmlsbD0iI2ZmZmZmZiI+6K+RPC90ZXh0Pjwvc3ZnPg== // @match *://*/* // @run-at document-start // @inject-into content // @noframes // @grant GM_getValue // @grant GM_setValue // @grant GM_deleteValue // @grant GM_xmlhttpRequest // @grant GM_registerMenuCommand // @connect translate.googleapis.com // @connect fanyi-api.baidu.com // @connect api-free.deepl.com // @connect api.deepl.com // @connect api.openai.com // @connect openrouter.ai // @connect api.deepseek.com // @connect localhost // @connect 127.0.0.1 // ==/UserScript== (() => { "use strict"; const SCRIPT_ID = "fit-lite"; const SETTINGS_KEY = `${SCRIPT_ID}:settings:v1`; const CACHE_KEY = `${SCRIPT_ID}:cache:v1`; const CACHE_LIMIT = 1200; // Bump when extraction/translation pipeline behavior changes: cached output // from older (possibly buggy) revisions must not keep resurfacing. const CACHE_PIPELINE_REV = "r2"; const SCAN_DELAY_MS = 80; const SCAN_MAX_WAIT_MS = 300; const HOUSEKEEPING_INTERVAL_MS = 10 * 60 * 1000; const CACHE_SAVE_DELAY_MS = 800; const DEFAULT_FLOATING_TOP = 335; const TWITTER_LONG_TEXT_LIMIT = 30000; const GOOGLE_SINGLE_REQUEST_CHARS = 3000; const GOOGLE_BATCH_REQUEST_CHARS = 4000; const BAIDU_SINGLE_REQUEST_CHARS = 1800; const GOOGLE_REQUEST_SPACING_MS = 650; const GOOGLE_BLOCK_BASE_COOLDOWN_MS = 45 * 1000; const GOOGLE_BLOCK_MAX_COOLDOWN_MS = 10 * 60 * 1000; const GOOGLE_ARRAY_ENDPOINT = "https://translate.googleapis.com/translate_a/t"; const MAX_ITEM_ATTEMPTS = 2; const RETRY_REQUEUE_DELAY_MS = 1500; const DIAG_EVENT_LIMIT = 300; const DIAG_SWEEP_LIMIT = 120; const DIAG_SAMPLES_PER_REASON = 8; const DIAG_INSPECT_LIMIT = 2000; // Lazy mode preload distance: content within this many px of the viewport // is translated ahead of time; anything farther waits until scrolled near. const LAZY_PRELOAD_MARGIN_PX = 300; const SCAN_ROOT_LIMIT = 36; // Per-scan raw collection cap. Generous because huge apps (Discord) carry // hundreds of legit-but-filtered-later elements (member lists) in doc order // before the actual chat content — a low cap starves what comes after. const SCAN_CANDIDATE_LIMIT = 2000; const INLINE_CANDIDATE_LIMIT = 260; const MUTATION_SCAN_NODE_LIMIT = 80; const PENDING_QUEUE_LIMIT = 420; const LEGACY_SPEED_DEFAULTS = { batchSize: 10, batchChars: 3200, maxConcurrency: 3, }; const STYLE_OPTIONS = [ ["immersive", "沉浸式"], ["classic", "经典边框"], ["soft", "柔和底色"], ["underline", "下划线"], ]; const FONT_SIZE_OPTIONS = [ ["default", "默认"], ["small", "小"], ["normal", "标准"], ["large", "大"], ["xlarge", "特大"], ]; const BLOCK_SELECTOR = [ "p", "li", "dd", "dt", "blockquote", "figcaption", "caption", "th", "td", "h1", "h2", "h3", "h4", "h5", "h6", "article", "section", "main", "div", ].join(","); // Chat metadata embedded inside message text blocks (Telegram web nests the // timestamp/view-count INSIDE the message element). Stripped during text // extraction and ignored when judging whether a block is a "container". const EXTRACT_META_SELECTOR = ".time,.views"; const EXTRACT_IGNORED_TAGS = new Set(["SCRIPT", "STYLE", "NOSCRIPT", "TEMPLATE", "SVG", "MATH"]); // Telegram web (tweb) renders link-preview cards, quotes and the timestamp // INSIDE the .message element. The message text is treated as one leaf block // with those sub-blocks skipped during extraction — they have their own // child candidates and translate separately. const TELEGRAM_PAGE_SELECTOR = ".bubbles-date-group"; const TELEGRAM_MESSAGE_SELECTOR = ".message"; const TELEGRAM_SUBBLOCK_SELECTOR = ".webpage,.quote-like,.reply,.document-wrapper,.audio,.poll,.reply-markup," + // Reaction rows (heart/thumbs-up icons with counters) live inside .message // too — their counts leaked into the translated source as stray digits. ".reactions,.reaction,reactions-element,reaction-element"; const TWITTER_TEXT_SELECTOR = "[data-testid='tweetText']"; const TWITTER_NATIVE_TRANSLATE_SELECTOR = [ "[data-testid='tweetTranslate']", "[data-testid='translateTweet']", "[data-testid='tweet-text-translate-button']", ].join(","); const UI_TEXT_SELECTOR = ["button", "input[placeholder]", "textarea[placeholder]", "label", "summary"].join(","); const INLINE_TEXT_SELECTOR = ["span", "a", "strong", "em", "small"].join(","); const NESTED_BLOCK_SELECTOR = BLOCK_SELECTOR; const BLOCK_BOUNDARY_TAGS = new Set([ "P", "LI", "DD", "DT", "BLOCKQUOTE", "FIGCAPTION", "CAPTION", "TH", "TD", "TR", "H1", "H2", "H3", "H4", "H5", "H6", "ARTICLE", "SECTION", "MAIN", "DIV", "UL", "OL", "HEADER", "FOOTER", "ASIDE", "NAV", "TABLE", "HR", ]); const EXCLUDE_SELECTOR = [ "script", "style", "noscript", "template", "textarea", "input", "select", "button", "pre", "code", "kbd", "samp", "var", "svg", "math", // nav/footer/aside used to be wholesale-excluded, but they hold real // content users want bilingual (Discord's channel list is a