Last active
May 3, 2020 08:39
-
-
Save zombie110year/4e6e33890de8880d99bd8cfb7ff03a6c to your computer and use it in GitHub Desktop.
使用 KaTeX 渲染一些页面上的公式:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name Katex Here | |
| // @namespace https://js.zombie110year.top | |
| // @version 0.1 | |
| // @description 当页面加载完成后,手动选择是否用 KaTeX 渲染网页上的一些公式 | |
| // @author zombie110year | |
| // @grant none | |
| // @run-at context-menu | |
| // @include * | |
| // @require https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.js#sha384=y23I5Q6l+B6vatafAwxRu/0oK/79VlbSz7Q9aiSZUvyWYIYsd+qj+o24G5ZU2zJz | |
| // @require https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/contrib/auto-render.min.js#sha384=kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI | |
| // ==/UserScript== | |
| (() => { | |
| const KaTeXOPTION = { | |
| delimiters: [ | |
| { left: "$$", right: "$$", display: true }, | |
| { left: "\\(", right: "\\)", display: false }, | |
| { left: "\\[", right: "\\]", display: true }, | |
| { left: "$", right: "$", display: false }, | |
| ], | |
| throwOnError: false, | |
| }; | |
| async function main() { | |
| loadCSS().catch(err => console.error(err)); | |
| katexHere(document.body); | |
| } | |
| /** | |
| * 复刻 KaTeX 官网上的例子,添加 HTML head 元素 | |
| */ | |
| async function loadCSS() { | |
| const url = "https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css"; | |
| const sha384 = "zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq"; | |
| let el_style = document.createElement("link"); | |
| el_style.setAttribute("rel", "stylesheet"); | |
| el_style.setAttribute("href", url); | |
| el_style.setAttribute("integrity", `sha384-${sha384}`); | |
| el_style.setAttribute("crossorigin", "anonymous"); | |
| document.head.append(el_style); | |
| console.debug("style loaded"); | |
| } | |
| async function katexHere(el) { | |
| renderMathInElement(el, KaTeXOPTION); | |
| } | |
| main(); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment