OpenAI-ChatGPT LaTeX Auto Render (with MathJax V2)

Add auto LaTeX math render on OpenAI ChatGPT page.

As of 2022-12-05. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         OpenAI-ChatGPT LaTeX Auto Render (with MathJax V2)
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Add auto LaTeX math render on OpenAI ChatGPT page.
// @author       Scruel
// @match        https://chat.openai.com/chat
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var renderDelay = 1000;

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

        const headElement = document.getElementsByTagName('head')[0] || document.documentElement;
        headElement.insertBefore(scriptElement , headElement.firstChild);
        waitLoaded();
    }

    function waitLoaded() {
        if (!MathJax.hasOwnProperty('Hub')) {
            // console.log("Loading...")
            window.setTimeout(waitLoaded, 200);
        } else {
            renderLatex();
        }
    }

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

    function renderLatex() {
        if (!submitButton.disabled) {
            // console.log("Rendering...")
            MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
        }
        renderTrigger();
    }

    window.MathJax = {
        tex2jax: {
            inlineMath: [['$', '$']],
            displayMath  : [['$$', '$$']]
        },
        CommonHTML: { linebreaks: { automatic: true } },
        "HTML-CSS": { linebreaks: { automatic: true } },
        SVG: { linebreaks: { automatic: true } }
    };

    const submitButton = document.querySelector('main form button[class*="Submit"]');
    addScript('https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS_CHTML')
})();