CC98 Tools - MathJax

为CC98网页版添加MathJax数学公式支持

  1. // ==UserScript==
  2. // @name CC98 Tools - MathJax
  3. // @namespace https://www.cc98.org/
  4. // @version 0.1.3
  5. // @description 为CC98网页版添加MathJax数学公式支持
  6. // @author ml98
  7. // @match https://www.cc98.org/*
  8. // @require https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js
  9. // @require https://gf.qytechs.cn/scripts/2199-waitforkeyelements/code/waitForKeyElements.js?version=6349
  10. // ==/UserScript==
  11.  
  12. // ref
  13. // https://docs.mathjax.org/en/latest/web/configuration.html
  14. // https://docs.mathjax.org/en/v2.7-latest/advanced/dynamic.html
  15. // https://docs.mathjax.org/en/latest/advanced/typeset.html
  16. // https://stackoverflow.com/a/45763146
  17. // https://www.cc98.org/topic/2803718/695#4
  18.  
  19. var log = console.log; //function(){};
  20.  
  21. function loadMathJaxV2(){
  22. log("loadMathJax");
  23. let script = document.createElement("script");
  24. script.type = "text/x-mathjax-config";
  25. script.innerHTML =
  26. `MathJax.Hub.Config({
  27. // skipStartupTypeset: true,
  28. messageStyle: "none",
  29. "fast-preview": {disabled: true},
  30. extensions: ["tex2jax.js", "TeX/AMSmath.js"],
  31. jax: ["input/TeX", "output/HTML-CSS"],
  32. tex2jax: {
  33. inlineMath: [ ["$", "$"], ["[m]", "[/m]"]],
  34. displayMath: [ ['$$','$$'], ["[math]", "[/math]"] ],
  35. processEscapes: true,
  36. preview: "none"
  37. }
  38. });`;
  39. document.head.appendChild(script);
  40.  
  41. script = document.createElement('script');
  42. script.id = "MathJax-script";
  43. script.src = 'https://cdn.jsdelivr.net/npm/mathjax@2.7.8/MathJax.js?config=TeX-MML-AM_CHTML';
  44. document.head.appendChild(script);
  45. }
  46.  
  47. function loadMathJaxV3(){
  48. log("loadMathJaxScript");
  49. let script = document.createElement('script');
  50. script.type = "text/x-mathjax-config";
  51. script[(window.opera ? "innerHTML" : "text")] =
  52. `window.MathJax = {
  53. tex: { inlineMath: [ ["{{","}}"] ],
  54. displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
  55. processEscapes: true
  56. },
  57. svg: { fontCache: 'global'}
  58. };`;
  59. document.head.appendChild(script);
  60.  
  61. script = document.createElement('script');
  62. script.id = "MathJax-script";
  63. script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3.1.2/es5/tex-mml-chtml.js';
  64. document.head.appendChild(script);
  65. }
  66.  
  67. function refresh(){
  68. log('refresh');
  69. if(!unsafeWindow.MathJax) init();
  70. else MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
  71. }
  72.  
  73. function addRefreshButton(){
  74. 'use strict';
  75. log("addRefreshButton");
  76. let essayProp = document.querySelector("#essayProp");
  77. if(essayProp){
  78. let btn = document.createElement("div");
  79. btn.className = "followTopic";
  80. btn.style = "width: 4.5rem;";
  81. btn.onclick = refresh;
  82. btn.textContent = "渲染公式";
  83. essayProp.appendChild(btn);
  84. }
  85. }
  86.  
  87. function init(){
  88. 'use strict';
  89. log("%cCC98 Tools - MathJax", "font-size: large");
  90. if(init.flag) return;
  91. init.flag = true;
  92. setTimeout(loadMathJaxV2, 100);
  93. setTimeout(addRefreshButton, 100);
  94. }
  95.  
  96. waitForKeyElements("#\\31", init, true);
  97. waitForKeyElements(".ubb-preview", refresh, false);
  98. waitForKeyElements(".mde-preview", refresh, false);

QingJ © 2025

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