|
| 1 | +(function () { |
| 2 | + |
| 3 | + var CDN = { |
| 4 | + 'cdnjs.cloudflare.com': { |
| 5 | + api: 'https://api.cdnjs.com/libraries/mathjax?fields=version', |
| 6 | + version: 'version', |
| 7 | + mathjax: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/' |
| 8 | + }, |
| 9 | + |
| 10 | + 'cdn.rawgit.com': { |
| 11 | + api: 'https://api.github.com/repos/mathjax/mathjax/releases/latest', |
| 12 | + version: 'tag_name', |
| 13 | + mathjax: 'https://cdn.rawgit.com/mathjax/MathJax/' |
| 14 | + }, |
| 15 | + |
| 16 | + 'cdn.jsdelivr.net': { |
| 17 | + api: 'https://api.jsdelivr.com/v1/jsdelivr/libraries?name=mathjax&lastversion=*', |
| 18 | + version: 'lastversion', |
| 19 | + mathjax: 'https://cdn.jsdelivr.net/mathjax/' |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + function Error(message) { |
| 24 | + if (console && console.log) console.log(message); |
| 25 | + } |
| 26 | + |
| 27 | + function getScript() { |
| 28 | + if (document.currentScript) return document.currentScript; |
| 29 | + var scripts = document.getElementsByTagName("script"); |
| 30 | + for (var i = 0, m = scripts.length; i < m; i++) { |
| 31 | + var script = scripts[i]; |
| 32 | + for (var cdn in CDN) {if (CDN.hasOwnProperty(cdn)) { |
| 33 | + var url = CDN[cdn].mathjax; |
| 34 | + if (script.src && script.src.substr(0,url.length) === url) return script; |
| 35 | + }} |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + function getCDN(script) { |
| 40 | + if (!script) return; |
| 41 | + var cdn = script.src.replace(/https:\/\//,'').replace(/[\/\?].*/,''); |
| 42 | + return CDN[cdn]; |
| 43 | + } |
| 44 | + |
| 45 | + var cookiePattern = /(?:^|;\s*)mjx\.latest=([^;]*)(?:;|$)/; |
| 46 | + function getVersion() { |
| 47 | + var match; |
| 48 | + try {match = cookiePattern.exec(document.cookie)} catch (err) {} |
| 49 | + if (match && match[1] !== '') return match[1]; |
| 50 | + } |
| 51 | + function setVersion(version) { |
| 52 | + cookie = 'mjx.latest=' + version; |
| 53 | + var time = new Date(); |
| 54 | + time.setDate(time.getDate() + 7); |
| 55 | + cookie += '; expires=' + time.toGMTString(); |
| 56 | + cookie += '; path=/'; |
| 57 | + try {document.cookie = cookie} catch (err) {} |
| 58 | + } |
| 59 | + |
| 60 | + function getXMLHttpRequest() { |
| 61 | + if (window.XMLHttpRequest) return new XMLHttpRequest(); |
| 62 | + if (window.ActiveXObject) { |
| 63 | + try {return new ActiveXObject("Msxml2.XMLHTTP")} catch (err) {} |
| 64 | + try {return new ActiveXObject("Microsoft.XMLHTTP")} catch (err) {} |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + function loadMathJax(url) { |
| 69 | + var script = document.createElement('script'); |
| 70 | + script.type = 'text/javascript'; |
| 71 | + script.async = true; |
| 72 | + script.src = url; |
| 73 | + var head = document.head || document.getElementsByTagName('head')[0] || document.body; |
| 74 | + if (head) { |
| 75 | + head.appendChild(script); |
| 76 | + } else { |
| 77 | + Error("Can't find the document <head> element"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + function loadDefaultMathJax() { |
| 82 | + var script = getScript(); |
| 83 | + if (script) { |
| 84 | + loadMathJax(script.src.replace(/\/latest\.js/, "/MathJax.js")); |
| 85 | + } else { |
| 86 | + Error("Can't determine the URL for loading MathJax"); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + function getLatestMathJax(cdn,config,unpacked) { |
| 91 | + var request = getXMLHttpRequest(); |
| 92 | + if (request) { |
| 93 | + request.onreadystatechange = function() { |
| 94 | + if (request.readyState === 4) { |
| 95 | + if (request.status === 200) { |
| 96 | + var json = JSON.parse(request.responseText); |
| 97 | + if (json instanceof Array) json = json[0]; |
| 98 | + var version = json[cdn.version]; |
| 99 | + if (version.substr(0,1) === '2') { |
| 100 | + setVersion(version); |
| 101 | + loadMathJax(cdn.mathjax + json[cdn.version] + unpacked + '/MathJax.js' + config); |
| 102 | + return; |
| 103 | + } |
| 104 | + } else { |
| 105 | + Error("Problem aquiring MathJax version: status = " + request.status); |
| 106 | + } |
| 107 | + laodDefaultMathJax(); |
| 108 | + } |
| 109 | + } |
| 110 | + request.open('GET', cdn.api, true); |
| 111 | + request.send(null); |
| 112 | + } else { |
| 113 | + Error("Can't create XMLHttpRequest object"); |
| 114 | + loadDefaultMathJax(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + var script = getScript(); |
| 119 | + var cdn = getCDN(script); |
| 120 | + if (cdn) { |
| 121 | + var config = script.src.replace(/.*?(\?|$)/, "$1"); |
| 122 | + var unpacked = (script.src.match(/\/unpacked\/latest\.js/) ? "/unpacked" : ""); |
| 123 | + var version = getVersion(); |
| 124 | + if (version) { |
| 125 | + loadMathJax(cdn.mathjax + version + unpacked + '/MathJax.js' + config); |
| 126 | + } else { |
| 127 | + getLatestMathJax(cdn, config, unpacked); |
| 128 | + } |
| 129 | + } else { |
| 130 | + loadDefaultMathJax(); |
| 131 | + } |
| 132 | + |
| 133 | +})(); |
0 commit comments