NoTranslate auto enable

Remove the "notranslate" class from all elements and add "notranslate" to elements with class containing "code"

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         NoTranslate auto enable
// @namespace    http://tampermonkey.net/
// @license      Apache-2.0 license
// @version      1.0
// @description  Remove the "notranslate" class from all elements and add "notranslate" to elements with class containing "code"
// @author       LorisYounger
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to remove notranslate class
    function removeNotranslate(node) {
        if (node.nodeType === 1 && node.classList && node.classList.contains('notranslate')) {
            node.classList.remove('notranslate');
        }
        Array.from(node.children).forEach(removeNotranslate);
    }

    // Function to add notranslate class to elements with class containing 'code'
    function addNotranslateToCode(node) {
        if (node.nodeType === 1 && node.classList && /.*code.*/.test(node.className) && !node.classList.contains('notranslate')) {
            node.classList.add('notranslate');
        }
        Array.from(node.children).forEach(addNotranslateToCode);
    }

    // Initial processing
    removeNotranslate(document.body);
    addNotranslateToCode(document.body);

    // Create a MutationObserver instance to handle dynamic content
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.type === 'childList') {
                mutation.addedNodes.forEach(function(node) {
                    if (node.nodeType === 1) {
                        removeNotranslate(node);
                        addNotranslateToCode(node);
                    }
                });
            } else if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
                let oldClass = mutation.oldValue;
                let newClass = mutation.target.className;

                if (/.*code.*/.test(newClass) && !/.*code.*/.test(oldClass)) {
                    addNotranslateToCode(mutation.target);
                } else if (!/.*code.*/.test(newClass) && /.*code.*/.test(oldClass)) {
                    removeNotranslate(mutation.target);
                }

                if (newClass.includes('notranslate') && !oldClass.includes('notranslate')) {
                    addNotranslateToCode(mutation.target);
                } else if (!newClass.includes('notranslate') && oldClass.includes('notranslate')) {
                    removeNotranslate(mutation.target);
                }
            }
        });
    });

    // Start observing the document with the configured parameters
    observer.observe(document.body, { attributes: true, attributeOldValue: true, childList: true, subtree: true });
})();