OpenAI-ChatGPT LaTeX Auto Render (with MathJax V2)

Add auto LaTeX math render on OpenAI ChatGPT page.

当前为 2022-12-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name OpenAI-ChatGPT LaTeX Auto Render (with MathJax V2)
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Add auto LaTeX math render on OpenAI ChatGPT page.
  6. // @author Scruel
  7. // @match https://chat.openai.com/chat
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. var renderDelay = 1000;
  15.  
  16. function addScript(url) {
  17. const scriptElement = document.createElement('script');
  18. scriptElement.src = url;
  19. scriptElement.type = 'text/javascript';
  20. scriptElement.async = true;
  21.  
  22. const headElement = document.getElementsByTagName('head')[0] || document.documentElement;
  23. headElement.insertBefore(scriptElement , headElement.firstChild);
  24. waitLoaded();
  25. }
  26.  
  27. function waitLoaded() {
  28. if (!MathJax.hasOwnProperty('Hub')) {
  29. console.log("Loading...")
  30. window.setTimeout(waitLoaded, 200);
  31. } else {
  32. renderLatex();
  33. }
  34. }
  35.  
  36. function renderTrigger() {
  37. setTimeout(renderLatex, renderDelay);
  38. }
  39.  
  40. function renderLatex() {
  41. const submitButton = document.querySelector('main form textarea+button');
  42. console.log(submitButton)
  43. if (submitButton && !submitButton.disabled) {
  44. console.log("Rendering...")
  45. MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
  46. }
  47. renderTrigger();
  48. }
  49.  
  50. window.MathJax = {
  51. tex2jax: {
  52. inlineMath: [['$', '$']],
  53. displayMath : [['$$', '$$']]
  54. },
  55. CommonHTML: { linebreaks: { automatic: true } },
  56. "HTML-CSS": { linebreaks: { automatic: true } },
  57. SVG: { linebreaks: { automatic: true } }
  58. };
  59.  
  60. addScript('https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS_CHTML')
  61. })();