OpenAI-ChatGPT LaTeX Auto Render (with MathJax V2)

Add auto LaTeX math render on OpenAI ChatGPT page.

Versione datata 06/12/2022. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         OpenAI-ChatGPT LaTeX Auto Render (with MathJax V2)
// @namespace    http://tampermonkey.net/
// @version      0.2.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() {
        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();
    }

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

    addScript('https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS_CHTML')
})();