MathJax selectable text

Forces all MathJax-using sites to output not images but HTML with selectable text

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @author      daniel.z.tg
// @description Forces all MathJax-using sites to output not images but HTML with selectable text
// @grant       GM_addStyle
// @license     Unlicense
// @name        MathJax selectable text
// @namespace   https://github.com/danielzgtg
// @version     202601251140110001
// @match       https://*/*
// ==/UserScript==

/*
 * Features:
 * - Makes MathJax-outputted equations selectable on all websites
 * - Hooks into MathJax to change its settings, which should compatibly integrate into all versions
 * - Allows disabling webfonts in Firefox settings or uBlock Origin
 *
 * Anti-features:
 * - "Math Processing Error" is very rare now, but if it happens, just temporarily disable this userscript
 * - The new rendered math is HTML so it looks uglier than the LaTeX-looking images
 */

GM_addStyle(`
.katex .katex-mathml
  position:initial !important;
}
.katex-html
  display:none !important;
}
.MathJax {
  > nobr {
    display: none !important;
  }
  > span.MJX_Assistive_MathML {
    clip: revert !important;
    overflow: revert !important;
    position: revert !important;
    width: revert !important;
    height: revert !important;
    padding: revert !important;
    display: revert !important;
    user-select: revert !important;
    font-size: 120% !important;
  }
}
`);

location.host === "www.researchgate.net" && GM_addStyle(`
div#pdf-to-html-container div.c.x1.y1.w2.h2 {
  overflow:initial!important;
}
`);

(() => {
  // Quora delays loading math, so MutationObserver's needed to wait for it
  const observer = new MutationObserver(callback);
  const observerOptions = { childList: true };
  function callback() {
    observer.disconnect();
    setTimeout(deferred, 0);
  }
  callback();
  function deferred() {
    const Hub = document.defaultView.MathJax?.Hub;
    if (!Hub) {
      observer.observe(document.head, observerOptions);
      return;
    }
    const OldConfig = Hub.Config.bind(Hub);
    const mixin = {
      availableFonts: null,
      preferredFont: null,
      webFont: null,
      fonts: null,
      imageFont: null,
      // Shouldn't matter much with webfonts disabled
      // I prefer "Noto Sans", but I put "Arial" here in case Windows users need it
      undefinedFamily: 'Arial',
    };
    OldConfig({ 'HTML-CSS': { ...mixin }, });
    Hub.Config = siteMathJaxConfig => {
      const overridenMathJaxConfig = {...siteMathJaxConfig};
      if ('HTML-CSS' in overridenMathJaxConfig) {
        overridenMathJaxConfig['HTML-CSS'] = {
          ...overridenMathJaxConfig['HTML-CSS'],
          ...mixin,
        }
      }
      OldConfig(overridenMathJaxConfig);
    };
  }
})();