Disable Auto Translation for Specific Elements

防止自动翻译数学公式、表格、图表等复杂内容

当前为 2024-09-25 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Disable Auto Translation for Specific Elements
// @namespace    http://1998x-stack.github.io
// @version      1.0
// @description  防止自动翻译数学公式、表格、图表等复杂内容
// @author       XM
// @match        *://*/*
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    /**
     * 给指定的元素及其子元素添加translate属性
     * @param {NodeListOf<Element>} elements - 需要处理的元素列表
     */
    function addTranslateNoToElements(elements) {
        elements.forEach(element => {
            element.setAttribute('translate', 'no');
            
            // 获取该元素的所有子元素并添加translate属性
            const childTags = element.getElementsByTagName('*');
            Array.from(childTags).forEach(tag => {
                tag.setAttribute('translate', 'no');
            });
        });
    }

    // 获取所有需要添加translate属性的元素
    const mathElems = document.querySelectorAll('.ltx_Math, .ltx_equationgroup, .ltx_equation, .ltx_figure, .ltx_table');
    const captionElems = document.querySelectorAll('.ltx_caption.ltx_centering');

    // 给这些元素及其子元素添加translate="no"属性
    addTranslateNoToElements(mathElems);
    addTranslateNoToElements(captionElems);

})();