让谷歌浏览器不翻译代码和公式

针对谷歌翻译,可以自定义指定标签、关键词不翻译

目前为 2025-02-05 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Make Chrome not Translate Code
  3. // @name:zh-CN 让谷歌浏览器不翻译代码和公式
  4. // @name:en Make Chrome not Translate Code
  5. // @description For Google Translate, you can customize and specify tags and keywords without translating
  6. // @author @amormaid
  7. // @run-at document-end
  8. // @namespace http://tampermonkey.net/
  9. // @version 1.5
  10. // @description:zh-cn 针对谷歌翻译,可以自定义指定标签、关键词不翻译
  11. // @description:en For Google Translate, you can customize and specify tags and keywords without translating
  12. // @match *://*/*
  13. // @license GPL
  14. // @grant none
  15. // ==/UserScript==
  16.  
  17.  
  18.  
  19.  
  20. (function () {
  21. 'use strict'
  22.  
  23. console.log('not translate ready!');
  24. let action_id = undefined
  25. function setNotTranslate() {
  26. const list = [
  27. ...(document.getElementsByTagName("math") || []),
  28. ...(document.getElementsByTagName("svg") || []),
  29. ...(document.getElementsByTagName("tex-math") || []),
  30. ...(document.getElementsByClassName("MathJax"))
  31. ];
  32. Array.from(list).forEach(e => e.classList.add('notranslate'));
  33. Array.from(list).forEach(e => e.setAttribute('translate', 'no'));
  34. console.log('not translate complete')
  35. }
  36. action_id = setTimeout(setNotTranslate, 500);
  37.  
  38. const observerOptions = {
  39. childList: true,
  40. subtree: true,
  41. };
  42.  
  43. const observer = new MutationObserver((records, observer) => {
  44. for (const record of records) {
  45. // console.log('record is ', record.target.tagName )
  46. if (["math","svg","tex-math"].includes(record.target.tagName.toLowerCase()) || ["MathJax"].some(class_name => record.target.classList.contains(class_name))) {
  47. action_id && clearTimeout(action_id)
  48. action_id = setTimeout(setNotTranslate, 500);
  49. }
  50.  
  51. }
  52. });
  53. observer.observe(document.body, observerOptions);
  54.  
  55.  
  56.  
  57. })();