Invisible Google Translate Widget

Makes the Google Translate widget vanish without a trace.

À partir de 2025-01-21. Voir la dernière version.

// ==UserScript==
// @name         Invisible Google Translate Widget
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Makes the Google Translate widget vanish without a trace.
// @author       UniverseDev
// @license      GPL-3.0-or-later
// @match        *://*/*
// @grant        unsafeWindow
// @icon         https://ssl.gstatic.com/translate/favicon.ico
// ==/UserScript==

(function () {
    'use strict';

    function isWidgetLanguageEnglish() {
        try {
            const iframe = document.querySelector('iframe.goog-te-banner-frame');
            if (!iframe) return false;

            const innerDoc = iframe.contentDocument || iframe.contentWindow?.document;
            if (!innerDoc) return false;

            const selectedLang = innerDoc.querySelector('.goog-te-combo')?.value;
            return selectedLang === 'en';
        } catch (error) {
            return false;
        }
    }

    function injectHideCSS() {
        const style = document.createElement('style');
        style.type = 'text/css';
        style.textContent = `
            #google_translate_element,
            .goog-te-banner-frame,
            .goog-te-balloon-frame,
            .goog-te-spinner-pos,
            .goog-gt-vt,
            #goog-gt-original-text,
            .skiptranslate > iframe,
            .VIpgJd-ZVi9od-aZ2wEe-wOHMyf,
            .VIpgJd-ZVi9od-aZ2wEe {
                display: none !important;
                visibility: hidden !important;
                height: 0 !important;
                border: none !important;
                box-shadow: none !important;
            }
            body {
                top: auto !important;
                position: static !important;
            }
        `;
        document.head.appendChild(style);
    }

    function initializeTranslateWidget() {
        if (isWidgetLanguageEnglish()) {
            injectHideCSS();
            return;
        }

        if (document.querySelector('#google_translate_element')) return;

        const script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit';
        document.head.appendChild(script);

        const translateDiv = document.createElement('div');
        translateDiv.id = 'google_translate_element';
        document.body.appendChild(translateDiv);

        unsafeWindow.googleTranslateElementInit = function () {
            new google.translate.TranslateElement({ pageLanguage: 'auto' }, 'google_translate_element');
        };
    }

    function addPassiveEventListener(eventName, callback) {
        window.addEventListener(eventName, callback, { passive: true });
    }

    addPassiveEventListener('load', () => {
        initializeTranslateWidget();
        injectHideCSS();
    });

})();