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

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

目前为 2020-04-02 提交的版本。查看 最新版本

  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. function addNotranslate (selector, loop, interval) {
  66. $(selector).attr("class", function(i,origValue){
  67. return origValue + " notranslate";
  68. });
  69. }
  70. function run () {
  71. console.log($);
  72. for(let i = 0; i < behaviorList.length; i++) {
  73. //console.log(behaviorList[i]);
  74. let selector = '';
  75. switch (behaviorList[i].type){
  76. case 'class':
  77. selector = "."+behaviorList[i].param;
  78. break;
  79. case 'id':
  80. selector = "#"+behaviorList[i].param;
  81. break;
  82. case 'element':
  83. selector = behaviorList[i].param;
  84. break;
  85. }
  86. addNotranslate(selector, behaviorList[i].loop, behaviorList[i].interval);
  87. }
  88. }
  89. function main(){
  90. $(function(){
  91. $("body").keyup(function(event){
  92. if (event.ctrlKey && event.keyCode === 81){
  93. if(confirm('Will add "notranslate" to some elements on his page. Continue?')){
  94. run();
  95. console.log('finish');
  96. }
  97. }
  98. });
  99. });
  100. }
  101. main();
  102. })();