MathJax Support

为网页添加MathJax支持,优化性能

Fra og med 13.10.2024. Se den nyeste version.

// ==UserScript==
// @name         MathJax Support
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  为网页添加MathJax支持,优化性能
// @author       Snowballl11
// @license      MIT
// @match        *://*/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    // 检查页面是否已经加载了MathJax
    if (window.MathJax) {
        console.log('MathJax已经存在,嘎!');
        return;
    }

    // 创建MathJax配置
    window.MathJax = {
        tex: {
            inlineMath: [['$', '$'], ['\\(', '\\)']],
            displayMath: [['$$', '$$'], ['\\[', '\\]']],
            processEscapes: true
        },
        options: {
            skipHtmlTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
    };

    // 创建并插入MathJax脚本
    var script = document.createElement('script');
    script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
    script.async = true;
    document.head.appendChild(script);

    // 添加一个MathJax加载完成的回调
    script.onload = function() {
        console.log('MathJax加载完成,嘎!');

        // 使用防抖函数来限制typeset的调用频率
        var debounceTypeset = debounce(function() {
            if (typeof MathJax !== 'undefined') {
                MathJax.typeset();
            }
        }, 1000);

        // 创建一个MutationObserver来监视特定元素的变化
        var observer = new MutationObserver(function(mutations) {
            debounceTypeset();
        });

        // 配置observer,只观察子节点的变化
        var config = { childList: true, subtree: true };

        // 尝试找到文章内容的容器元素,这里假设它有一个特定的ID或类名
        var contentContainer = document.querySelector('#article-content') || document.querySelector('.post-content');
        if (contentContainer) {
            observer.observe(contentContainer, config);
        } else {
            console.log('未找到文章容器,改为观察整个body,嘎!');
            observer.observe(document.body, config);
        }
    };

    // 防抖函数
    function debounce(func, wait) {
        var timeout;
        return function() {
            var context = this, args = arguments;
            clearTimeout(timeout);
            timeout = setTimeout(function() {
                func.apply(context, args);
            }, wait);
        };
    }
})();

/*
MIT License

Copyright (c) [2024] [Snowballl11]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/