notranslate-防止翻译代码段和公式

避免文献及博客阅读时代码段与公式被不正确地翻译

  1. // ==UserScript==
  2. // @name:zh-CN notranslate-防止翻译代码段和公式
  3. // @name:zh-TW notranslate-防止翻譯代碼段和公式
  4. // @name:en notranslate-Prevent translating snippets and formulas
  5. // @name notranslate-Prevent translating snippets and formulas
  6. // @namespace http://tampermonkey.net/
  7. // @version 0.3
  8. // @description:zh-CN 避免文献及博客阅读时代码段与公式被不正确地翻译
  9. // @description:zh-TW 避免文獻及博客閲讀時代碼段與公式被不正塙地翻譯
  10. // @description:en A tampermonkey script aims to prevent unnecessary translation of code segment and math equations.
  11. // @description A tampermonkey script aims to prevent unnecessary translation of code segment and math equations.
  12. // @author winding
  13. // @supportURL https://github.com/windingwind/notranslate/blob/master/README.md
  14. // @include *
  15. // @require http://code.jquery.com/jquery-3.4.1.min.js
  16. // @grant GM_setValue
  17. // @grant GM_getValue
  18. // ==/UserScript==
  19.  
  20. (function() {
  21. 'use strict';
  22. /*
  23. You can add your settings here.
  24. "type" can be "id", "class", or "element".
  25. "param" is how you trace the target element.
  26. */
  27. const behaviorList = [
  28. {
  29. 'type':'class',
  30. 'param':'prettyprint',
  31. },
  32. {
  33. 'type':'class',
  34. 'param':'mjx-chtml',
  35. },
  36. {
  37. 'type':'class',
  38. 'param':'MJXc-display',
  39. },
  40. {
  41. 'type':'class',
  42. 'param':'MathJax_Display',
  43. },
  44. {
  45. 'type':'class',
  46. 'param':'math-container',
  47. },
  48. {
  49. 'type':'class',
  50. 'param':'MathJax',
  51. },
  52. {
  53. 'type':'class',
  54. 'param':'katex--display',
  55. },
  56. {
  57. 'type':'class',
  58. 'param':'syntaxhighlighter',
  59. },
  60. {
  61. 'type':'class',
  62. 'param':'code_cell',
  63. },
  64. {
  65. // github markdown
  66. 'type':'class',
  67. 'param':'highlight',
  68. },
  69. ];
  70. function addNotranslate (selector, loop, interval) {
  71. $(selector).attr("class", function(i,origValue){
  72. return origValue + " notranslate";
  73. });
  74. }
  75. function run () {
  76. console.log($);
  77. for(let i = 0; i < behaviorList.length; i++) {
  78. //console.log(behaviorList[i]);
  79. let selector = '';
  80. switch (behaviorList[i].type){
  81. case 'class':
  82. selector = "."+behaviorList[i].param;
  83. break;
  84. case 'id':
  85. selector = "#"+behaviorList[i].param;
  86. break;
  87. case 'element':
  88. selector = behaviorList[i].param;
  89. break;
  90. }
  91. addNotranslate(selector, behaviorList[i].loop, behaviorList[i].interval);
  92. }
  93. }
  94. function main(){
  95. $(function(){
  96. $("body").keyup(function(event){
  97. if (event.ctrlKey && event.keyCode === 81){
  98. if(confirm('Will add "notranslate" to some elements on his page. Continue?')){
  99. run();
  100. console.log('finish');
  101. }
  102. }
  103. });
  104. });
  105. }
  106. main();
  107. })();