pdf-mathjax-loader.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* vim:fileencoding=utf-8
  2. *
  3. * Copyright (C) 2019 Kovid Goyal <kovid at kovidgoyal.net>
  4. *
  5. * Distributed under terms of the GPLv3 license
  6. */
  7. /*jshint esversion: 6 */
  8. (function() {
  9. "use strict";
  10. var base = document.currentScript ? document.currentScript.getAttribute('data-mathjax-path') : null;
  11. var for_pdf_renderer = !!base;
  12. function on_mathjax_finish() {
  13. if (for_pdf_renderer) document.title = "mathjax-load-complete";
  14. else document.documentElement.dispatchEvent(new CustomEvent("calibre-mathjax-typeset-done"));
  15. }
  16. // also do any changes in mathjax.pyj for the in-browser reader
  17. window.MathJax = {};
  18. window.MathJax.options = {
  19. renderActions: {
  20. // disable the mathjax context menu
  21. addMenu: [0, '', ''],
  22. },
  23. };
  24. window.MathJax.loader = {
  25. load: ['input/tex-full', 'input/asciimath', 'input/mml', 'output/chtml'],
  26. };
  27. window.MathJax.startup = {
  28. ready: () => {
  29. MathJax.startup.defaultReady();
  30. MathJax.startup.promise.then(on_mathjax_finish);
  31. },
  32. };
  33. for (const s of document.scripts) {
  34. if (s.type === "text/x-mathjax-config") {
  35. var es = document.createElement('script');
  36. es.text = s.text;
  37. document.head.appendChild(es);
  38. document.head.removeChild(es);
  39. }
  40. }
  41. if (for_pdf_renderer) {
  42. if (!base.endsWith('/')) base += '/';
  43. var script = document.createElement('script');
  44. script.type = 'text/javascript';
  45. script.setAttribute('async', 'async');
  46. script.onerror = function (ev) {
  47. console.log('Failed to load MathJax script: ' + ev.target.src);
  48. };
  49. script.src = base + 'startup.js';
  50. document.head.appendChild(script);
  51. }
  52. })();