Update adding MathJax dynamically and GreaseMonkey scripts

This commit is contained in:
Davide P. Cervone 2011-03-07 20:10:54 -05:00
parent 5c40227d79
commit 5c2dbb3315
3 changed files with 46 additions and 68 deletions

View File

@ -10,12 +10,8 @@ if ((window.unsafeWindow == null ? window : unsafeWindow).MathJax == null) {
(document.getElementsByTagNameNS == null ? false : (document.getElementsByTagNameNS == null ? false :
(document.getElementsByTagNameNS("http://www.w3.org/1998/Math/MathML","math").length > 0))) { (document.getElementsByTagNameNS("http://www.w3.org/1998/Math/MathML","math").length > 0))) {
var script = document.createElement("script"); var script = document.createElement("script");
script.src = "http://www.yoursite.edu/MathJax/MathJax.js"; // put your URL here script.src = "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML-full";
var config = 'MathJax.Hub.Config({' + var config = 'MathJax.Hub.Startup.onload()';
'extensions:["mml2jax.js"],' +
'jax:["input/MathML","output/HTML-CSS"]' +
'});' +
'MathJax.Hub.Startup.onload()';
if (window.opera) {script.innerHTML = config} else {script.text = config} if (window.opera) {script.innerHTML = config} else {script.text = config}
document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("head")[0].appendChild(script);
} }

View File

@ -9,27 +9,23 @@ if ((window.unsafeWindow == null ? window : unsafeWindow).MathJax == null) {
// //
// Replace the images with MathJax scripts of type math/tex // Replace the images with MathJax scripts of type math/tex
// //
var images = document.getElementsByTagName('img'); var images = document.getElementsByTagName('img'), count = 0;
for (var i = images.length - 1; i >= 0; i--) { for (var i = images.length - 1; i >= 0; i--) {
var img = images[i]; var img = images[i];
if (img.className === "tex") { if (img.className === "tex") {
var script = document.createElement("script"); script.type = "math/tex"; var script = document.createElement("script"); script.type = "math/tex";
if (window.opera) {script.innerHTML = img.alt} else {script.text = img.alt} if (window.opera) {script.innerHTML = img.alt} else {script.text = img.alt}
img.parentNode.replaceChild(script,img); img.parentNode.replaceChild(script,img); count++;
} }
} }
if (count) {
// //
// Load MathJax and have it process the page // Load MathJax and have it process the page
// //
var script = document.createElement("script"); var script = document.createElement("script");
script.src = "http://www.yoursite.edu/MathJax/MathJax.js"; // put your URL here script.src = "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML-full";
var config = 'MathJax.Hub.Config({' + var config = 'MathJax.Hub.Startup.onload()';
'config: ["MMLorHTML.js"],' +
'extensions:["TeX/noErrors.js","TeX/noUndefined.js",' +
'"TeX/AMSmath.js","TeX/AMSsymbols.js"],' +
'jax:["input/TeX"]' +
'});' +
'MathJax.Hub.Startup.onload()';
if (window.opera) {script.innerHTML = config} else {script.text = config} if (window.opera) {script.innerHTML = config} else {script.text = config}
document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("head")[0].appendChild(script);
}
} }

View File

@ -20,9 +20,7 @@ handler yourself, to let it know that it is OK to typeset the
mathematics on the page. You accomplish this by calling the mathematics on the page. You accomplish this by calling the
:meth:`MathJax.Hub.Startup.onload()` method as part of your MathJax :meth:`MathJax.Hub.Startup.onload()` method as part of your MathJax
startup script. To do this, you will need to give MathJax an in-line startup script. To do this, you will need to give MathJax an in-line
configuration, so you will not be able to use the configuration.
``config/MathJax.js`` file (though you can add it to your in-line
configuration's `config` array).
Here is an example of how to load and configure MathJax dynamically: Here is an example of how to load and configure MathJax dynamically:
@ -31,7 +29,7 @@ Here is an example of how to load and configure MathJax dynamically:
(function () { (function () {
var script = document.createElement("script"); var script = document.createElement("script");
script.type = "text/javascript"; script.type = "text/javascript";
script.src = "/MathJax/MathJax.js"; // use the location of your MathJax script.src = "http://cdn.mathjax.org/mathjax/latest/MathJax.js";
var config = 'MathJax.Hub.Config({' + var config = 'MathJax.Hub.Config({' +
'extensions: ["tex2jax.js"],' + 'extensions: ["tex2jax.js"],' +
@ -45,13 +43,12 @@ Here is an example of how to load and configure MathJax dynamically:
document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("head")[0].appendChild(script);
})(); })();
Be sure to set the ``src`` to the correct URL for your copy of You can adjust the ``config`` variable to your needs, but be careful to get
MathJax. You can adjust the ``config`` variable to your needs, but be the commas right. The ``window.opera`` test is because some versions of
careful to get the commas right. The ``window.opera`` test is because
Opera doesn't handle setting ``script.text`` properly, while Internet Opera doesn't handle setting ``script.text`` properly, while Internet
Explorer doesn't handle setting the ``innerHTML`` of a script tag. Explorer doesn't handle setting the ``innerHTML`` of a script tag.
Here is a version that uses the ``config/MathJax.js`` file to Here is a version that uses the ``config=filename`` method to
configure MathJax: configure MathJax:
.. code-block:: javascript .. code-block:: javascript
@ -59,10 +56,9 @@ configure MathJax:
(function () { (function () {
var script = document.createElement("script"); var script = document.createElement("script");
script.type = "text/javascript"; script.type = "text/javascript";
script.src = "/MathJax/MathJax.js"; // use the location of your MathJax script.src = "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML-full";
var config = 'MathJax.Hub.Config({ config: "MathJax.js" }); ' + var config = 'MathJax.Hub.Startup.onload();';
'MathJax.Hub.Startup.onload();';
if (window.opera) {script.innerHTML = config} if (window.opera) {script.innerHTML = config}
else {script.text = config} else {script.text = config}
@ -71,13 +67,12 @@ configure MathJax:
})(); })();
Note that the **only** reliable way to configure MathJax is to use an Note that the **only** reliable way to configure MathJax is to use an
in-line configuration of the type discussed above. You should **not** in-line configuration block of the type discussed above. You should
call :meth:`MathJax.Hub.Config()` directly in your code, as it will **not** call :meth:`MathJax.Hub.Config()` directly in your code, as it will
not run at the correct time --- it will either run too soon, in which not run at the correct time --- it will either run too soon, in which case
case ``MathJax`` may not be defined and the function will throw an ``MathJax`` may not be defined and the function will throw an error, or it
error, or it will run too late, after MathJax has already finished its will run too late, after MathJax has already finished its configuration
configuration process, so your changes will not have the desired process, so your changes will not have the desired effect.
effect.
MathJax and GreaseMonkey MathJax and GreaseMonkey
@ -97,8 +92,7 @@ Note, however, that most browsers don't allow you to insert a script
that loads a ``file://`` URL into a page that comes from the web (for that loads a ``file://`` URL into a page that comes from the web (for
security reasons). That means that you can't have your GreaseMonkey security reasons). That means that you can't have your GreaseMonkey
script load a local copy of MathJax, so you have to refer to a script load a local copy of MathJax, so you have to refer to a
server-based copy. In the scripts below, you need to insert the URL server-based copy. The MathJax CDN works nicely for this.
of a copy of MathJax from your own server.
---- ----
@ -122,12 +116,8 @@ IE+MathPlayer.
(document.getElementsByTagNameNS == null ? false : (document.getElementsByTagNameNS == null ? false :
(document.getElementsByTagNameNS("http://www.w3.org/1998/Math/MathML","math").length > 0))) { (document.getElementsByTagNameNS("http://www.w3.org/1998/Math/MathML","math").length > 0))) {
var script = document.createElement("script"); var script = document.createElement("script");
script.src = "http://www.yoursite.edu/MathJax/MathJax.js"; // put your URL here script.src = "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML-full";
var config = 'MathJax.Hub.Config({' + var config = 'MathJax.Hub.Startup.onload()';
'extensions:["mml2jax.js"],' +
'jax:["input/MathML","output/HTML-CSS"]' +
'});' +
'MathJax.Hub.Startup.onload()';
if (window.opera) {script.innerHTML = config} else {script.text = config} if (window.opera) {script.innerHTML = config} else {script.text = config}
document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("head")[0].appendChild(script);
} }
@ -153,29 +143,25 @@ converting the math images to their original TeX code.
// //
// Replace the images with MathJax scripts of type math/tex // Replace the images with MathJax scripts of type math/tex
// //
var images = document.getElementsByTagName('img'); var images = document.getElementsByTagName('img'), count = 0;
for (var i = images.length - 1; i >= 0; i--) { for (var i = images.length - 1; i >= 0; i--) {
var img = images[i]; var img = images[i];
if (img.className === "tex") { if (img.className === "tex") {
var script = document.createElement("script"); script.type = "math/tex"; var script = document.createElement("script"); script.type = "math/tex";
if (window.opera) {script.innerHTML = img.alt} else {script.text = img.alt} if (window.opera) {script.innerHTML = img.alt} else {script.text = img.alt}
img.parentNode.replaceChild(script,img); img.parentNode.replaceChild(script,img); count++;
} }
} }
if (count) {
// //
// Load MathJax and have it process the page // Load MathJax and have it process the page
// //
var script = document.createElement("script"); var script = document.createElement("script");
script.src = "http://www.yoursite.edu/MathJax/MathJax.js"; // put your URL here script.src = "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML-full";
var config = 'MathJax.Hub.Config({' + var config = 'MathJax.Hub.Startup.onload()';
'config: ["MMLorHTML.js"],' +
'extensions:["TeX/noErrors.js","TeX/noUndefined.js",' +
'"TeX/AMSmath.js","TeX/AMSsymbols.js"],' +
'jax:["input/TeX"]' +
'});' +
'MathJax.Hub.Startup.onload()';
if (window.opera) {script.innerHTML = config} else {script.text = config} if (window.opera) {script.innerHTML = config} else {script.text = config}
document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("head")[0].appendChild(script);
} }
}
**Source**: `mathjax_wikipedia.user.js <_static/mathjax_wikipedia.user.js>`_ **Source**: `mathjax_wikipedia.user.js <_static/mathjax_wikipedia.user.js>`_