-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlatex-render-toggle.user.js
More file actions
200 lines (178 loc) · 6.39 KB
/
latex-render-toggle.user.js
File metadata and controls
200 lines (178 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// ==UserScript==
// @name LaTeX Render Toggle
// @author eWloYW8
// @version 1.4
// @description 在网页上添加开关动态开启/关闭 LaTeX 渲染(MathJax、KaTeX、Wikipedia)。
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
let latexEnabled = true;
function extractKatexSource(k) {
if (k.dataset.latexSrc) return k.dataset.latexSrc;
const annotation = k.querySelector(".katex-mathml annotation");
if (annotation && annotation.textContent.trim()) {
k.dataset.latexSrc = annotation.textContent.trim();
return k.dataset.latexSrc;
}
const math = k.querySelector(".katex-mathml math");
if (math && math.lastChild && math.lastChild.nodeType === Node.TEXT_NODE) {
k.dataset.latexSrc = math.lastChild.textContent.trim();
return k.dataset.latexSrc;
}
return null;
}
function extractMathJaxSource(node) {
if (node.dataset.latexSrc) return node.dataset.latexSrc;
let source = null;
const script = node.nextElementSibling;
if (script && script.tagName === 'SCRIPT' && script.type.startsWith('math/tex')) {
source = script.textContent.trim();
}
if (!source && node.dataset.tex) {
source = node.dataset.tex;
}
if (!source) {
const innerScript = node.querySelector('script[type^="math/tex"]');
if (innerScript && innerScript.textContent.trim()) {
source = innerScript.textContent.trim();
}
}
if (source) {
node.dataset.latexSrc = source;
return source;
}
return null;
}
function extractWikipediaSource(el) {
if (el.dataset.latexSrc) return el.dataset.latexSrc;
let source = null;
const annotation = el.querySelector("annotation[encoding='application/x-tex']");
if (annotation && annotation.textContent.trim()) {
source = annotation.textContent.trim();
}
if (!source) {
const img = el.querySelector("img.mwe-math-fallback-image-inline");
if (img && img.alt) {
source = img.alt.trim();
}
}
if (source) {
el.dataset.latexSrc = source;
return source;
}
return null;
}
function getAssociatedScript(mathJaxElement) {
const script = mathJaxElement.nextElementSibling;
if (script && script.tagName === 'SCRIPT' && script.type.startsWith('math/tex')) {
return script;
}
return mathJaxElement.querySelector('script[type^="math/tex"]');
}
function disableLatexFor(el) {
if (el.dataset.processed === '1') return;
let latex = null;
let associatedScript = null;
if (el.classList.contains('katex')) {
latex = extractKatexSource(el);
} else if (el.classList.contains('MathJax')) {
latex = extractMathJaxSource(el);
associatedScript = getAssociatedScript(el);
} else if (el.classList.contains('mwe-math-element')) {
latex = extractWikipediaSource(el);
}
if (!latex) return;
if (!el.previousElementSibling || !el.previousElementSibling.classList.contains('latex-raw')) {
const span = document.createElement('span');
span.className = 'latex-raw';
span.textContent = latex;
span.style.whiteSpace = 'pre-wrap';
span.style.fontFamily = 'Consolas, monospace';
el.insertAdjacentElement('beforebegin', span);
}
el.style.display = 'none';
el.dataset.processed = '1';
if (associatedScript) {
associatedScript.style.display = 'none';
associatedScript.dataset.processed = '1';
}
}
function enableLatexFor(el) {
const raw = el.previousElementSibling;
if (raw && raw.classList.contains('latex-raw')) {
raw.remove();
}
el.style.display = '';
el.dataset.processed = '0';
if (el.classList.contains('MathJax')) {
const associatedScript = getAssociatedScript(el);
if (associatedScript) {
associatedScript.style.display = '';
associatedScript.dataset.processed = '0';
}
}
}
function applyToggle() {
const nodes = document.querySelectorAll('.katex, .MathJax, .mwe-math-element');
nodes.forEach(el => latexEnabled ? enableLatexFor(el) : disableLatexFor(el));
}
const btn = document.createElement('button');
btn.textContent = 'Disable LaTeX';
btn.style.cssText = `
position: fixed;
bottom: 20px;
right: 20px;
padding: 8px 12px;
background: #444;
color: #fff;
border: none;
border-radius: 6px;
z-index: 999999;
cursor: pointer;
opacity: 0.9;
`;
btn.style.display = 'none';
document.body.appendChild(btn);
function toggleLatex() {
latexEnabled = !latexEnabled;
btn.textContent = latexEnabled ? 'Disable LaTeX' : 'Enable LaTeX';
applyToggle();
}
btn.onclick = toggleLatex;
document.addEventListener('keydown', function(event) {
if (event.altKey && event.key === 'l') {
if (btn.style.display !== 'none') {
event.preventDefault();
toggleLatex();
}
}
});
function checkAndShowButton() {
if (btn.style.display !== 'none') return;
const selector = '.katex, .MathJax, .mwe-math-element';
if (document.querySelector(selector)) {
btn.style.display = '';
}
}
const observer = new MutationObserver(mutations => {
let needsToggle = false;
const selector = '.katex, .MathJax, .mwe-math-element';
for (const m of mutations) {
for (const n of m.addedNodes) {
if (!(n instanceof HTMLElement)) continue;
if (n.matches(selector) || n.querySelector(selector)) {
needsToggle = true;
}
}
}
if (needsToggle) {
applyToggle();
checkAndShowButton();
}
});
observer.observe(document.body, { childList: true, subtree: true });
applyToggle();
checkAndShowButton();
})();