Disable LaTeX on Discourse

Prevent LaTeX rendering on Discourse forums

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Disable LaTeX on Discourse
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Prevent LaTeX rendering on Discourse forums
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Helper to override MathJax and KaTeX render functions
    function blockRenderers() {
        if (window.MathJax) {
            console.log("[Tampermonkey] Blocking MathJax...");
            window.MathJax = {
                typeset: function() {},
                typesetPromise: function() { return Promise.resolve(); },
                startup: { promise: Promise.resolve() },
                config: {}
            };
        }

        if (window.katex) {
            console.log("[Tampermonkey] Blocking KaTeX...");
            window.katex = {
                render: function() {},
                renderToString: function() { return ''; }
            };
        }
    }

    // Observe new script tags that try to load MathJax or KaTeX
    const observer = new MutationObserver((mutations) => {
        for (let mutation of mutations) {
            for (let node of mutation.addedNodes) {
                if (node.tagName === 'SCRIPT' && node.src &&
                    (node.src.includes('MathJax') || node.src.includes('katex'))) {
                    console.log("[Tampermonkey] Blocking script:", node.src);
                    node.type = 'javascript/blocked';
                    node.parentNode.removeChild(node);
                }
            }
        }
    });

    observer.observe(document.documentElement, {
        childList: true,
        subtree: true
    });

    // Run blocker early and often
    blockRenderers();
    setInterval(blockRenderers, 1000); // Re-block in case the page tries to reload them
})();