OpenAI-ChatGPT LaTeX Auto Render (with MathJax V2)

Auto typeset LaTeX math formulas on OpenAI ChatGPT page.

2022-12-08 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name               OpenAI-ChatGPT LaTeX Auto Render (with MathJax V2)
// @namespace          http://tampermonkey.net/
// @version            0.3.1
// @author             Scruel (https://github.com/scruel)
// @description        Auto typeset LaTeX math formulas on OpenAI ChatGPT page.
// @description:zh-CN  自动渲染 OpenAI 的 ChatGPT 页面上的 LaTeX 数学公式。
// @match              https://chat.openai.com/chat
// @grant              none
// ==/UserScript==

'use strict';

async function addScript(url) {
    const scriptElement = document.createElement('script');
    scriptElement.src = url;
    scriptElement.async = true;

    const headElement = document.getElementsByTagName('head')[0] || document.documentElement;
    headElement.appendChild(scriptElement);
    await waitScriptLoaded();
}

function waitScriptLoaded() {
  return new Promise(async (resolve, reject) => {
      const resolver = () => {
          if (MathJax.hasOwnProperty('Hub')) {
              resolve();
              return;
          }
          if (window.ChatLatex.loadCount > 100) {
              setTipsElementText("Load MathJax failed, try refresh.");
              reject("Load MathJax failed, try refresh.");
              return;
          }
          window.ChatLatex.loadCount += 1;
          window.setTimeout(resolver, 200);
      }
      resolver();
  });
}

function renderTrigger() {
    setTimeout(renderLatex, window.renderDelay);
}

function renderLatex() {
    const submitButton = document.querySelector('main form textarea+button');
    // console.log(submitButton)
    if (submitButton && !submitButton.disabled) {
        // console.log("Rendering...")
        MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
    }
    renderTrigger();
}

function showTipsElement() {
    const tipsElement = window.ChatLatex.tipsElement;
    tipsElement.style.position = "fixed";
    tipsElement.style.left = "10px";
    tipsElement.style.bottom = "10px";
    tipsElement.style.background = '#333';
    tipsElement.style.color = '#fff';
    document.body.appendChild(tipsElement);
}

function setTipsElementText(text) {
    window.ChatLatex.tipsElement.innerHTML = text;
}

function hideTipsElement(timeout=3) {
    window.setTimeout(() => {window.ChatLatex.tipsElement.hidden=true}, 3000);
}

(async function() {
    window.ChatLatex = {
        tipsElement: document.createElement("div"),
        loadCount: 0
    }
    window.MathJax = {
        tex2jax: {
            inlineMath: [['$', '$']],
            displayMath  : [['$$', '$$']]
        },
        CommonHTML: { linebreaks: { automatic: true } },
        "HTML-CSS": { linebreaks: { automatic: true } },
        SVG: { linebreaks: { automatic: true } },
    };

    showTipsElement();
    setTipsElementText("Loading MathJax...");
    await addScript('https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS_CHTML');
    setTipsElementText("MathJax Loaded.");
    hideTipsElement();

    window.renderDelay = 1000;
    renderTrigger();
})();