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

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

  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.6
  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. const root = document.getElementsByTagName('html')[0];
  25. root.classList.remove('notranslate');
  26. root.removeAttribute('translate');
  27.  
  28. let action_id = undefined
  29. function setNotTranslate() {
  30. const list = [
  31. ...(document.getElementsByTagName("math") || []),
  32. ...(document.getElementsByTagName("svg") || []),
  33. ...(document.getElementsByTagName("tex-math") || []),
  34. ...(document.getElementsByClassName("MathJax"))
  35. ];
  36. Array.from(list).forEach(e => e.classList.add('notranslate'));
  37. Array.from(list).forEach(e => e.setAttribute('translate', 'no'));
  38. console.log('not translate complete')
  39. }
  40. action_id = setTimeout(setNotTranslate, 500);
  41.  
  42. const observerOptions = {
  43. childList: true,
  44. subtree: true,
  45. };
  46.  
  47. const observer = new MutationObserver((records, observer) => {
  48. for (const record of records) {
  49. // console.log('record is ', record.target.tagName )
  50. if (["math","svg","tex-math"].includes(record.target.tagName.toLowerCase()) || ["MathJax"].some(class_name => record.target.classList.contains(class_name))) {
  51. action_id && clearTimeout(action_id)
  52. action_id = setTimeout(setNotTranslate, 500);
  53. }
  54.  
  55. }
  56. });
  57. observer.observe(document.body, observerOptions);
  58.  
  59.  
  60.  
  61. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址