Disable LaTeX Rendering

阻止LaTeX渲染并显示原始代码

  1. // ==UserScript==
  2. // @name Disable LaTeX Rendering
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 阻止LaTeX渲染并显示原始代码
  6. // @author YourName
  7. // @match *://*/*
  8. // @run-at document-start
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 阻止MathJax自动渲染
  16. window.MathJax = {
  17. startup: { typeset: false },
  18. tex: {
  19. inlineMath: [['@@', '@@']], // 无效定界符
  20. displayMath: [['@@@', '@@@']]
  21. }
  22. };
  23.  
  24. // 阻止KaTeX自动渲染
  25. document.addEventListener('DOMContentLoaded', function() {
  26. if (window.renderMathInElement) {
  27. window.renderMathInElement = function() {};
  28. }
  29. });
  30.  
  31. // 显示原始LaTeX代码
  32. window.addEventListener('DOMContentLoaded', function() {
  33. // 处理MathJax和KaTeX的公式容器
  34. const replaceWithOriginal = (selector, contentAttr) => {
  35. document.querySelectorAll(selector).forEach(element => {
  36. const code = document.createElement('pre');
  37. code.textContent = element.getAttribute(contentAttr);
  38. code.style.color = 'black';
  39. code.style.backgroundColor = '#f0f0f0';
  40. code.style.padding = '5px';
  41. element.replaceWith(code);
  42. });
  43. };
  44.  
  45. // 处理MathJax公式
  46. replaceWithOriginal('[data-original-content]', 'data-original-content');
  47. // 处理KaTeX公式
  48. replaceWithOriginal('[data-original]', 'data-original');
  49.  
  50. // 处理<script>公式标签
  51. document.querySelectorAll('script[type^="math/tex"]').forEach(script => {
  52. const pre = document.createElement('pre');
  53. pre.textContent = script.textContent;
  54. pre.style.color = 'black';
  55. pre.style.backgroundColor = '#f0f0f0';
  56. pre.style.padding = '5px';
  57. script.replaceWith(pre);
  58. });
  59.  
  60. // 处理行内公式定界符
  61. document.body.innerHTML = document.body.innerHTML
  62. .replace(/\\\(/g, 'LATEX-INLINE-START')
  63. .replace(/\\\)/g, 'LATEX-INLINE-END')
  64. .replace(/\\\[/g, 'LATEX-DISPLAY-START')
  65. .replace(/\\\]/g, 'LATEX-DISPLAY-END');
  66. });
  67.  
  68. // 动态内容处理
  69. new MutationObserver(mutations => {
  70. mutations.forEach(mutation => {
  71. mutation.addedNodes.forEach(node => {
  72. if (node.nodeType === Node.ELEMENT_NODE) {
  73. node.innerHTML = node.innerHTML
  74. .replace(/\\\(/g, 'LATEX-INLINE-START')
  75. .replace(/\\\)/g, 'LATEX-INLINE-END')
  76. .replace(/\\\[/g, 'LATEX-DISPLAY-START')
  77. .replace(/\\\]/g, 'LATEX-DISPLAY-END');
  78. }
  79. });
  80. });
  81. }).observe(document.body, {
  82. childList: true,
  83. subtree: true
  84. });
  85. })();