Disable Auto Translation for Specific Elements

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴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);

})();