您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Remove the "notranslate" class from all elements and add "notranslate" to elements with class containing "code"
// ==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 }); })();