Browse Source

Update mathjax loading

simov 2 years ago
parent
commit
8da2cfcfef
3 changed files with 24 additions and 6 deletions
  1. 1 0
      background/inject.js
  2. 1 6
      content/index.js
  3. 22 0
      content/mathjax.js

+ 1 - 0
background/inject.js

@@ -31,6 +31,7 @@ md.inject = ({storage: {state}}) => (id) => {
       state.content.syntax && '/vendor/prism.min.js',
       state.content.emoji && '/content/emoji.js',
       state.content.mermaid && ['/vendor/mermaid.min.js', '/content/mermaid.js'],
+      state.content.mathjax && ['/content/mathjax.js', '/vendor/mathjax/tex-mml-chtml.js'],
       '/content/index.js',
     ].filter(Boolean).flat(),
     injectImmediately: true

+ 1 - 6
content/index.js

@@ -155,12 +155,7 @@ function mount () {
             $('body').classList.add('_toc-left')
           }
           if (state.content.mathjax) {
-            dom.push(m('script', {
-              src: chrome.runtime.getURL('/content/mathjax.js')
-            }))
-            dom.push(m('script', {
-              src: chrome.runtime.getURL('/vendor/mathjax/tex-mml-chtml.js')
-            }))
+            mj.render()
           }
         }
       }

+ 22 - 0
content/mathjax.js

@@ -18,5 +18,27 @@ var MathJax = {
   positionToHash: false,
   options: {
     ignoreHtmlClass: 'tex2jax-ignore'
+  },
+  loader: {
+    paths: {mathjax: chrome.runtime.getURL('/vendor/mathjax/es5')},
+  },
+  startup: {
+    typeset: false
+  }
+}
+
+var mj = {
+  loading: false,
+  render: () => {
+    if (mj.loading) {
+      return
+    }
+    mj.loading = true
+    var timeout = setInterval(() => {
+      if (!!MathJax) {
+        clearInterval(timeout)
+        MathJax.typesetPromise()
+      }
+    }, 0)
   }
 }