Disable Auto Translation for Specific Elements

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

  1. // ==UserScript==
  2. // @name Disable Auto Translation for Specific Elements
  3. // @namespace http://1998x-stack.github.io
  4. // @version 1.0
  5. // @description 防止自动翻译数学公式、表格、图表等复杂内容
  6. // @author XM
  7. // @match *://*/*
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. /**
  15. * 给指定的元素及其子元素添加translate属性
  16. * @param {NodeListOf<Element>} elements - 需要处理的元素列表
  17. */
  18. function addTranslateNoToElements(elements) {
  19. elements.forEach(element => {
  20. element.setAttribute('translate', 'no');
  21. // 获取该元素的所有子元素并添加translate属性
  22. const childTags = element.getElementsByTagName('*');
  23. Array.from(childTags).forEach(tag => {
  24. tag.setAttribute('translate', 'no');
  25. });
  26. });
  27. }
  28.  
  29. // 获取所有需要添加translate属性的元素
  30. const mathElems = document.querySelectorAll('.ltx_Math, .ltx_equationgroup, .ltx_equation, .ltx_figure, .ltx_table');
  31. const captionElems = document.querySelectorAll('.ltx_caption.ltx_centering');
  32.  
  33. // 给这些元素及其子元素添加translate="no"属性
  34. addTranslateNoToElements(mathElems);
  35. addTranslateNoToElements(captionElems);
  36.  
  37. })();