CC98 Tools - MathJax

为CC98网页版添加MathJax数学公式支持

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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         CC98 Tools - MathJax
// @namespace    https://www.cc98.org/
// @version      0.1.3
// @description  为CC98网页版添加MathJax数学公式支持
// @author       ml98
// @match        https://www.cc98.org/*
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @require      https://greasyfork.org/scripts/2199-waitforkeyelements/code/waitForKeyElements.js?version=6349
// ==/UserScript==

// ref
// https://docs.mathjax.org/en/latest/web/configuration.html
// https://docs.mathjax.org/en/v2.7-latest/advanced/dynamic.html
// https://docs.mathjax.org/en/latest/advanced/typeset.html
// https://stackoverflow.com/a/45763146
// https://www.cc98.org/topic/2803718/695#4

var log = console.log; //function(){};

function loadMathJaxV2(){
    log("loadMathJax");
    let script = document.createElement("script");
    script.type = "text/x-mathjax-config";
    script.innerHTML =
        `MathJax.Hub.Config({
    // skipStartupTypeset: true,
    messageStyle: "none",
    "fast-preview": {disabled: true},
    extensions: ["tex2jax.js", "TeX/AMSmath.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
        inlineMath: [ ["$", "$"], ["[m]", "[/m]"]],
        displayMath: [ ['$$','$$'], ["[math]", "[/math]"] ],
        processEscapes: true,
        preview: "none"
    }
});`;
    document.head.appendChild(script);

    script = document.createElement('script');
    script.id = "MathJax-script";
    script.src = 'https://cdn.jsdelivr.net/npm/[email protected]/MathJax.js?config=TeX-MML-AM_CHTML';
    document.head.appendChild(script);
}

function loadMathJaxV3(){
    log("loadMathJaxScript");
    let script = document.createElement('script');
    script.type = "text/x-mathjax-config";
    script[(window.opera ? "innerHTML" : "text")] =
        `window.MathJax = {
    tex: { inlineMath: [ ["{{","}}"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
        processEscapes: true
    },
    svg: { fontCache: 'global'}
};`;
    document.head.appendChild(script);

    script = document.createElement('script');
    script.id = "MathJax-script";
    script.src = 'https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-mml-chtml.js';
    document.head.appendChild(script);
}

function refresh(){
    log('refresh');
    if(!unsafeWindow.MathJax) init();
    else MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
}

function addRefreshButton(){
    'use strict';
    log("addRefreshButton");
    let essayProp = document.querySelector("#essayProp");
    if(essayProp){
        let btn = document.createElement("div");
        btn.className = "followTopic";
        btn.style = "width: 4.5rem;";
        btn.onclick = refresh;
        btn.textContent = "渲染公式";
        essayProp.appendChild(btn);
    }
}

function init(){
    'use strict';
    log("%cCC98 Tools - MathJax", "font-size: large");
    if(init.flag) return;
    init.flag = true;
    setTimeout(loadMathJaxV2, 100);
    setTimeout(addRefreshButton, 100);
}

waitForKeyElements("#\\31", init, true);
waitForKeyElements(".ubb-preview", refresh, false);
waitForKeyElements(".mde-preview", refresh, false);