OpenAI-ChatGPT LaTeX Auto Render (with MathJax V2)

Add auto LaTeX math render on OpenAI ChatGPT page.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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.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')
})();