FASTEST 2025 Google Translate

Super-fast Google Translate with instant SPA/AJAX support, no hiding

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         FASTEST 2025 Google Translate
// @namespace    http://instagram.com/waterdustlab
// @version      1.2
// @description  Super-fast Google Translate with instant SPA/AJAX support, no hiding
// @match        *://*/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    let translateInstance; // Store instance for refresh

    function initTranslate() {
        const div = document.createElement('div');
        div.id = 'google_translate_element';
        document.body.appendChild(div);

        window.googleTranslateElementInit = function() {
            translateInstance = new google.translate.TranslateElement({
                pageLanguage: document.documentElement.lang || 'auto',
                includedLanguages: 'en', // Adjust as needed
                layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
                autoDisplay: true // Let toolbar show naturally
            }, 'google_translate_element');
        };

        // Load Google Translate script async for speed
        const script = document.createElement('script');
        script.src = 'https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit';
        script.async = true;
        document.head.appendChild(script);

        // MutationObserver for dynamic/SPA/AJAX content
        const observer = new MutationObserver((mutations) => {
            if (translateInstance && mutations.length > 0) {
                // Trigger refresh on Google's translate instance if available
                if (typeof translateInstance.restore === 'function') {
                    translateInstance.restore(); // Reset and re-translate
                } else if (typeof google.translate.TranslateElement === 'function') {
                    // Fallback: Re-init minimally
                    translateInstance = new google.translate.TranslateElement({
                        pageLanguage: document.documentElement.lang || 'auto',
                        includedLanguages: 'en',
                        layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
                        autoDisplay: true
                    }, 'google_translate_element');
                }
            }
        });

        // Observe body for changes (subtree for deep SPA updates)
        observer.observe(document.body, {
            childList: true,
            subtree: true,
            attributes: false // Minimal for speed
        });
    }

    // Wait for body if not ready (rare, but safe)
    if (document.body) {
        initTranslate();
    } else {
        const bodyObserver = new MutationObserver(() => {
            if (document.body) {
                bodyObserver.disconnect();
                initTranslate();
            }
        });
        bodyObserver.observe(document.documentElement, { childList: true });
    }
})();