14

令我不快的是,MediaWiki最近禁用了对全维基百科范围内的 TeX 公式的 MathJax(票证:T99369)渲染的支持。

由于我(和其他人,如果您浏览票证的讨论线程)发现其余选项(MathML,PNG)的渲染效果较差,我想将 MathJax “滑入”维基百科。

由于使用元素似乎无法通过 Wikipedia 中的自定义 JavaScript 设置直接加载更多 JavaScript 文件<script>,因此我不知道如何实现这一壮举。是不是,MathJax 最容易通过 CDN 包含在内。

我正在使用当前的 Edge 和 Firefox 浏览器,因此任何使用它们之一或两者的解决方案都将不胜感激!


同时,我找到了适用于 Firefox 的 Greasemonkey,如果给定一个合适的脚本,它或许可以做到这一点。由于我既不是 Greasemonkey 也不是 JavaScript 专家,所以任何关于如何继续编写这样一个脚本的提示都会有所帮助。

4

2 回答 2

11

作为注册用户,您可以执行以下操作:

用户首选项 => 外观下,打开“MathML with SVG or PNG fallback”模式。(其他两种模式需要稍微不同的脚本,但恕我直言,该模式是目前最好的选择。)

https://en.wikipedia.org/wiki/User:YOURHANDLE/common.js接下来在[不要忘记更改用户名!]编辑您的用户特定脚本页面,并将以下自定义脚本添加到其中:

// add to User:YOURNAME/common.js to get smooth MathJax rendering
var mathTags = $('.mwe-math-mathml-a11y');
if (mathTags.length > 0){ //only do something when there's math on the page
  window.MathJax = { //hook into MathJax's configuration
    AuthorInit: function () {
      MathJax.Hub.Register.StartupHook("End",function () { //when MathJax is done...
        MathJax.Hub.Queue(
            function(){
             mathTags.removeClass('mwe-math-mathml-a11y'); // .. make the span around MathML (now MathJax output) visible
             $('.mwe-math-fallback-image-inline').addClass('mwe-math-mathml-a11y'); //hide fallback images
            }
        );
      });
    }
  };
  mw.loader.load('https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=MML_HTMLorMML-full');//load MathJax with a suitable combined config file
}

此脚本仅在页面中有数学时加载 MathJax,渲染它,并且(在渲染完成时)用结果替换后备图像。

这样,您几乎没有抖动。从快速测试来看,这似乎适用于 Chrome 43、Firefox 39、IE8 和 Edge,以及 WebKit 2.6.2(因此应该适用于 Safari)。

于 2015-08-10T16:41:05.877 回答
2

似乎使用客户端 MathJax 的 GreaseMonkey 脚本现在列在MathJax 文档中。

从那里更新:

// ==UserScript==
// @name           MathJax in Wikipedia
// @namespace      http://www.mathjax.org/
// @description    Insert MathJax into Wikipedia pages
// @include        https://*.wikipedia.org/wiki/*
// ==/UserScript==

// replace the images with MathJax scripts of type math/tex
if (window.MathJax) throw "MathJax already loaded!";
var imgs = document.querySelectorAll('.mwe-math-fallback-image-inline')
if (!imgs.length) throw "no matches!";
imgs.forEach((img) => {
    var script = document.createElement("script");
    script.type = 'math/tex';
    script[window.opera ? 'innerHTML' : 'text'] = img.alt;
    img.parentNode.replaceChild(script, img);
})
// Load MathJax and have it process the page
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://cdn.mathjax.org/mathjax/2.7-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML-full';
document.querySelector('head').appendChild(script);
于 2018-04-28T10:13:16.830 回答