latest.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*************************************************************
  2. *
  3. * latest.js
  4. *
  5. * Replacement for cdn.mathjax.org/mathjax/latest that loads the
  6. * latest (2.x) version of MathJax from cdnjs, rawgit.com, or jsdelivr
  7. * depending on where it was loaded from.
  8. *
  9. * ---------------------------------------------------------------------
  10. *
  11. * Copyright (c) 2017-2018 The MathJax Consortium
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. (function () {
  26. var CDN = {
  27. 'cdnjs.cloudflare.com': {
  28. api: 'https://api.cdnjs.com/libraries/mathjax?fields=version',
  29. version: 'version',
  30. mathjax: 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/'
  31. },
  32. 'cdn.rawgit.com': {
  33. api: 'https://api.github.com/repos/mathjax/mathjax/releases/latest',
  34. version: 'tag_name',
  35. mathjax: 'https://cdn.rawgit.com/mathjax/MathJax/'
  36. },
  37. 'cdn.jsdelivr.net': {
  38. api: 'https://api.jsdelivr.com/v1/jsdelivr/libraries?name=mathjax&lastversion=*',
  39. version: 'lastversion',
  40. mathjax: 'https://cdn.jsdelivr.net/mathjax/'
  41. }
  42. };
  43. function Error(message) {
  44. if (console && console.log) console.log(message);
  45. }
  46. function getScript() {
  47. if (document.currentScript) return document.currentScript;
  48. var scripts = document.getElementsByTagName("script");
  49. for (var i = 0, m = scripts.length; i < m; i++) {
  50. var script = scripts[i];
  51. for (var cdn in CDN) {if (CDN.hasOwnProperty(cdn)) {
  52. var url = CDN[cdn].mathjax;
  53. if (script.src && script.src.substr(0,url.length) === url) return script;
  54. }}
  55. }
  56. }
  57. function getCDN(script) {
  58. if (!script) return;
  59. var cdn = script.src.replace(/https:\/\//,'').replace(/[\/\?].*/,'');
  60. return CDN[cdn];
  61. }
  62. var cookiePattern = /(?:^|;\s*)mjx\.latest=([^;]*)(?:;|$)/;
  63. function getVersion() {
  64. var match;
  65. try {match = cookiePattern.exec(document.cookie)} catch (err) {}
  66. if (match && match[1] !== '') return match[1];
  67. }
  68. function setVersion(version) {
  69. cookie = 'mjx.latest=' + version;
  70. var time = new Date();
  71. time.setDate(time.getDate() + 7);
  72. cookie += '; expires=' + time.toGMTString();
  73. cookie += '; path=/';
  74. try {document.cookie = cookie} catch (err) {}
  75. }
  76. function getXMLHttpRequest() {
  77. if (window.XMLHttpRequest) return new XMLHttpRequest();
  78. if (window.ActiveXObject) {
  79. try {return new ActiveXObject("Msxml2.XMLHTTP")} catch (err) {}
  80. try {return new ActiveXObject("Microsoft.XMLHTTP")} catch (err) {}
  81. }
  82. }
  83. function loadMathJax(url) {
  84. var script = document.createElement('script');
  85. script.type = 'text/javascript';
  86. script.async = true;
  87. script.src = url;
  88. var head = document.head || document.getElementsByTagName('head')[0] || document.body;
  89. if (head) {
  90. head.appendChild(script);
  91. } else {
  92. Error("Can't find the document <head> element");
  93. }
  94. }
  95. function loadDefaultMathJax() {
  96. var script = getScript();
  97. if (script) {
  98. loadMathJax(script.src.replace(/\/latest\.js/, "/MathJax.js"));
  99. } else {
  100. Error("Can't determine the URL for loading MathJax");
  101. }
  102. }
  103. function getLatestMathJax(cdn,config,unpacked) {
  104. var request = getXMLHttpRequest();
  105. if (request) {
  106. request.onreadystatechange = function() {
  107. if (request.readyState === 4) {
  108. if (request.status === 200) {
  109. var json = JSON.parse(request.responseText);
  110. if (json instanceof Array) json = json[0];
  111. var version = json[cdn.version];
  112. if (version.substr(0,2) === '2.') {
  113. setVersion(version);
  114. loadMathJax(cdn.mathjax + json[cdn.version] + unpacked + '/MathJax.js' + config);
  115. return;
  116. }
  117. } else {
  118. Error("Problem acquiring MathJax version: status = " + request.status);
  119. }
  120. loadDefaultMathJax();
  121. }
  122. }
  123. request.open('GET', cdn.api, true);
  124. request.send(null);
  125. } else {
  126. Error("Can't create XMLHttpRequest object");
  127. loadDefaultMathJax();
  128. }
  129. }
  130. var script = getScript();
  131. var cdn = getCDN(script);
  132. if (cdn) {
  133. var config = script.src.replace(/.*?(\?|$)/, "$1");
  134. config += (config ? '&' : '?') + 'latest';
  135. var unpacked = (script.src.match(/\/unpacked\/latest\.js/) ? "/unpacked" : "");
  136. var version = getVersion();
  137. if (version) {
  138. loadMathJax(cdn.mathjax + version + unpacked + '/MathJax.js' + config);
  139. } else {
  140. getLatestMathJax(cdn, config, unpacked);
  141. }
  142. } else {
  143. loadDefaultMathJax();
  144. }
  145. })();