This repository was archived by the owner on Jan 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
309 lines (270 loc) · 9.16 KB
/
content.js
File metadata and controls
309 lines (270 loc) · 9.16 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Inject page script for MathJax v3 extraction
function injectMathJaxPageScript() {
const script = document.createElement('script');
script.src = chrome.runtime.getURL('mathjax-api.js');
script.onload = function() {
this.remove();
};
document.documentElement.appendChild(script);
}
injectMathJaxPageScript();
// Listen for LaTeX messages from the page script
let lastMathJaxV3Latex = null;
window.addEventListener('message', function(event) {
if (event.source !== window) return;
if (event.data && event.data.type === 'HoverLatex_MathJaxV3') {
lastMathJaxV3Latex = event.data.latex;
}
});
let overlay;
let currentTarget = null;
function isWikipedia() {
const hostname = window.location.hostname;
return hostname.endsWith('.wikipedia.org') || hostname === 'www.wikiwand.com' || hostname === "wikimedia.org" || hostname.endsWith(".wikiversity.org") || hostname.endsWith(".wikibooks.org");
}
function findWikipediaTex(el) {
// Only work on Wikipedia/Wikiwand sites
if (!isWikipedia()) return null;
// Check if it's a Wikipedia math image
if (el.tagName === 'IMG' &&
(el.classList.contains('mwe-math') ||
el.classList.contains('mwe-math-fallback-image-inline') ||
el.classList.contains('mwe-math-fallback-image-display'))) {
const alt = el.getAttribute('alt');
if (alt && alt.trim()) {
// Remove leading '{\displaystyle' and trailing '}'
const match = alt.trim().match(/^\{\\displaystyle\s*([\s\S]*?)\}$/);
if (match) {
return match[1].trim();
}
return alt.trim();
}
}
return null;
}
function findMathJaxV3Tex(el) {
// Check for MathJax v3 containers
const mjxContainer = el.closest('mjx-container');
if (!mjxContainer) {
return null;
}
// Use the last received LaTeX from the page script
if (lastMathJaxV3Latex) {
return lastMathJaxV3Latex;
} else {
}
// Fallback: try to find any associated script elements nearby
let current = mjxContainer;
for (let i = 0; i < 5; i++) { // Check a few siblings
if (current.nextElementSibling) {
current = current.nextElementSibling;
if (current.tagName === 'SCRIPT' &&
(current.type === 'math/tex' || current.type === 'math/tex; mode=display')) {
return current.textContent.trim();
}
} else {
break;
}
}
return null;
}
function findAnnotationTex(el) {
const katexEl = el.closest('.katex');
if (!katexEl) return null;
const ann = katexEl.querySelector('.katex-mathml annotation[encoding="application/x-tex"]');
if (ann && ann.textContent.trim()) {
return ann.textContent.trim();
}
const dataLatex =
katexEl.getAttribute('data-tex') ||
katexEl.getAttribute('data-latex') ||
katexEl.getAttribute('aria-label');
if (dataLatex && dataLatex.trim()) return dataLatex.trim();
return null;
}
function findMathJaxTex(el) {
// Check for MathJax display equations
const mathJaxDisplay = el.closest('.MathJax_Display, .MJXc-display');
if (mathJaxDisplay) {
// Look for the script element after the display div
let sibling = mathJaxDisplay.nextElementSibling;
while (sibling) {
if (sibling.tagName === 'SCRIPT' &&
sibling.type === 'math/tex; mode=display') {
return sibling.textContent.trim();
}
sibling = sibling.nextElementSibling;
}
}
// Check for MathJax inline equations (various formats)
const mathJaxInline = el.closest('.MathJax, .mjx-chtml, .MathJax_CHTML, .MathJax_MathML');
if (mathJaxInline) {
// For traditional MathJax elements with IDs
if (mathJaxInline.id && mathJaxInline.id.includes('MathJax-Element-')) {
// Look for the script element after the MathJax span
let sibling = mathJaxInline.nextElementSibling;
while (sibling) {
if (sibling.tagName === 'SCRIPT' &&
sibling.type === 'math/tex') {
return sibling.textContent.trim();
}
sibling = sibling.nextElementSibling;
}
}
// For newer MathJax formats (mjx-chtml, MathJax_CHTML)
// Look for script elements with math/tex type
let sibling = mathJaxInline.nextElementSibling;
while (sibling) {
if (sibling.tagName === 'SCRIPT' &&
(sibling.type === 'math/tex' || sibling.type === 'math/tex; mode=display')) {
return sibling.textContent.trim();
}
sibling = sibling.nextElementSibling;
}
}
return null;
}
function createOverlay() {
overlay = document.createElement('div');
overlay.className = 'hoverlatex-overlay';
// Contingut HTML amb SVG i text
overlay.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20"
viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
<path d="M5 15H4a2 2 0 0 1-2-2V4
a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
</svg>
<span>Click to copy</span>
`;
document.body.appendChild(overlay);
}
function showOverlay(target, tex) {
if (!overlay) createOverlay();
overlay.dataset.tex = tex;
const rect = target.getBoundingClientRect();
const overlayWidth = overlay.offsetWidth;
const top = rect.top + window.scrollY - overlay.offsetHeight - 8;
const left = rect.left + window.scrollX + (rect.width / 2) - (overlayWidth / 2);
overlay.style.top = `${top}px`;
overlay.style.left = `${left}px`;
overlay.classList.add('visible');
}
function hideOverlay() {
if (overlay) {
overlay.classList.remove('visible');
}
}
function copyLatex(tex) {
navigator.clipboard.writeText(tex).then(() => {
overlay.classList.add('copied');
overlay.querySelector('span').textContent = 'Copied!';
setTimeout(() => {
overlay.classList.remove('copied');
overlay.querySelector('span').textContent = 'Click to copy';
}, 1500);
}).catch(err => {
console.error("[Copy LaTeX] Clipboard error:", err);
});
}
document.addEventListener('mouseover', (e) => {
// Check for Wikipedia math images first (only on Wikipedia/Wikiwand sites)
if (isWikipedia()) {
const wikipediaTex = findWikipediaTex(e.target);
if (wikipediaTex) {
currentTarget = e.target;
e.target.classList.add('hoverlatex-hover');
showOverlay(e.target, wikipediaTex);
return;
}
}
// Check for KaTeX elements
const katex = e.target.closest('.katex');
if (katex) {
const tex = findAnnotationTex(katex);
if (tex) {
currentTarget = katex;
katex.classList.add('hoverlatex-hover');
showOverlay(katex, tex);
return;
}
}
// Check for MathJax v3 elements
const mjxContainer = e.target.closest('mjx-container');
if (mjxContainer) {
const tex = findMathJaxV3Tex(mjxContainer);
if (tex) {
currentTarget = mjxContainer;
mjxContainer.classList.add('hoverlatex-hover');
showOverlay(mjxContainer, tex);
return;
}
}
// Check for MathJax elements
const mathJaxDisplay = e.target.closest('.MathJax_Display, .MJXc-display');
const mathJaxInline = e.target.closest('.MathJax, .mjx-chtml, .MathJax_CHTML, .MathJax_MathML');
if (mathJaxDisplay || mathJaxInline) {
const mathElement = mathJaxDisplay || mathJaxInline;
const tex = findMathJaxTex(mathElement);
if (tex) {
currentTarget = mathElement;
mathElement.classList.add('hoverlatex-hover');
showOverlay(mathElement, tex);
}
}
});
document.addEventListener('mouseout', (e) => {
if (currentTarget &&
!e.relatedTarget?.closest('.katex') &&
!e.relatedTarget?.closest('mjx-container') &&
!e.relatedTarget?.closest('.MathJax_Display, .MJXc-display') &&
!e.relatedTarget?.closest('.MathJax, .mjx-chtml, .MathJax_CHTML, .MathJax_MathML') &&
!(isWikipedia() &&
e.relatedTarget?.tagName === 'IMG' &&
(e.relatedTarget?.classList.contains('mwe-math') ||
e.relatedTarget?.classList.contains('mwe-math-fallback-image-inline') ||
e.relatedTarget?.classList.contains('mwe-math-fallback-image-display')))) {
currentTarget.classList.remove('hoverlatex-hover');
hideOverlay();
currentTarget = null;
}
});
document.addEventListener('click', (e) => {
// Check for Wikipedia math images first (only on Wikipedia/Wikiwand sites)
if (isWikipedia()) {
const wikipediaTex = findWikipediaTex(e.target);
if (wikipediaTex) {
copyLatex(wikipediaTex);
return;
}
}
// Check for KaTeX elements
const katex = e.target.closest('.katex');
if (katex) {
const tex = findAnnotationTex(katex);
if (tex) {
copyLatex(tex);
return;
}
}
// Check for MathJax v3 elements
const mjxContainer = e.target.closest('mjx-container');
if (mjxContainer) {
const tex = findMathJaxV3Tex(mjxContainer);
if (tex) {
copyLatex(tex);
return;
}
}
// Check for MathJax elements
const mathJaxDisplay = e.target.closest('.MathJax_Display, .MJXc-display');
const mathJaxInline = e.target.closest('.MathJax, .mjx-chtml, .MathJax_CHTML, .MathJax_MathML');
if (mathJaxDisplay || mathJaxInline) {
const mathElement = mathJaxDisplay || mathJaxInline;
const tex = findMathJaxTex(mathElement);
if (tex) {
copyLatex(tex);
}
}
});