FASTEST 2025 Google Translate

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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 });
    }
})();