-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
267 lines (226 loc) · 7.88 KB
/
content.js
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
console.log('[Content] Script starting...');
// Cargar KaTeX manualmente si es necesario
async function loadKaTeX() {
try {
if (typeof katex !== 'undefined') {
console.log('[Content] KaTeX already loaded');
return true;
}
console.log('[Content] Loading KaTeX manually...');
const script = document.createElement('script');
script.src = chrome.runtime.getURL('katex/katex.min.js');
await new Promise((resolve, reject) => {
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
console.log('[Content] KaTeX loaded successfully');
return true;
} catch (error) {
console.error('[Content] Error loading KaTeX:', error);
return false;
}
}
// Estado global
let isProcessing = false;
let katexChecked = false;
// Verificar disponibilidad de KaTeX
async function checkKaTeX() {
if (katexChecked) return typeof katex !== 'undefined';
try {
if (typeof katex === 'undefined') {
await loadKaTeX();
addCustomStyles();
}
katexChecked = true;
const available = typeof katex !== 'undefined';
console.log('[Content] KaTeX availability:', available);
return available;
} catch (error) {
console.error('[Content] KaTeX check error:', error);
return false;
}
}
function addCustomStyles() {
const style = document.createElement('style');
style.textContent = `
.katex {
color: inherit !important;
font-size: 1.15em !important;
text-align: left !important;
}
.katex-display {
margin: 0.5em 0 !important;
color: inherit !important;
display: block !important;
overflow-x: auto !important;
max-width: 100% !important;
}
.katex-html {
white-space: normal !important;
text-align: left !important;
}
.message-out .katex {
color: inherit !important;
}
.katex-block {
overflow-x: auto;
overflow-y: hidden;
padding: 2px 0;
}
`;
document.head.appendChild(style);
}
// Función para extraer y procesar fórmulas LaTeX del texto
function extractLatexFormulas(text) {
const displayRegex = /\\\[(.*?)\\\]/g;
const inlineRegex = /\\\((.*?)\\\)/g;
let formulas = [];
let match;
// Buscar fórmulas display mode
while ((match = displayRegex.exec(text)) !== null) {
formulas.push({
original: match[0],
formula: match[1],
displayMode: true,
index: match.index
});
}
// Buscar fórmulas inline
while ((match = inlineRegex.exec(text)) !== null) {
formulas.push({
original: match[0],
formula: match[1],
displayMode: false,
index: match.index
});
}
return formulas;
}
// Función para procesar un elemento con LaTeX
async function processLatexElement(element) {
// Obtener el texto completo combinando todos los spans
let text = '';
const spans = element.querySelectorAll('span');
if (spans.length > 0) {
spans.forEach(span => {
text += span.textContent + '\n';
});
} else {
text = element.innerText;
}
// Verificar si el texto contiene LaTeX
if (!text.includes("\\(") && !text.includes("\\[")) {
return;
}
// Limpiar saltos de linea
text = text.replace(/\n/g, " ");
console.log('[Content] Processing LaTeX text:', text);
// console.log(text)
try {
// Extraer fórmulas
const formulas = extractLatexFormulas(text);
if (formulas.length === 0) return;
console.log('[Content] Found formulas:', formulas.length);
// Crear nuevo contenedor manteniendo las clases originales
const container = document.createElement('span');
container.className = element.className;
let currentText = text;
let offset = 0;
// Procesar cada fórmula
for (const formula of formulas) {
try {
const rendered = katex.renderToString(formula.formula.trim(), {
throwOnError: false,
displayMode: formula.displayMode,
output: 'html',
strict: false,
trust: true
});
// Crear wrapper para mantener el estilo
const wrapper = document.createElement('span');
wrapper.style.cssText = 'color: inherit; display: inline-block;';
if (formula.displayMode) {
wrapper.style.cssText += 'width: 100%; overflow-x: auto;';
}
wrapper.innerHTML = rendered;
// Reemplazar la fórmula en el texto
const beforeFormula = currentText.substring(0, formula.index + offset);
const afterFormula = currentText.substring(formula.index + offset + formula.original.length);
currentText = beforeFormula + wrapper.outerHTML + afterFormula;
offset += wrapper.outerHTML.length - formula.original.length;
} catch (renderError) {
console.error('[Content] Render error for formula:', formula.formula, renderError);
}
}
container.innerHTML = currentText;
// Reemplazar el elemento original
if (element.parentNode) {
element.parentNode.replaceChild(container, element);
console.log('[Content] Element updated successfully');
}
} catch (error) {
console.error('[Content] Processing error:', error);
}
}
// Función para procesar todos los elementos con LaTeX
async function processLatexElements() {
if (isProcessing) return;
isProcessing = true;
try {
if (!await checkKaTeX()) {
console.error('[Content] KaTeX not available for processing');
return;
}
// console.log('[Content] Starting batch processing');
const elements = document.querySelectorAll('span._ao3e.selectable-text.copyable-text');
// console.log('[Content] Found elements:', elements.length);
for (const element of elements) {
await processLatexElement(element);
}
} catch (error) {
console.error('[Content] Batch processing error:', error);
} finally {
isProcessing = false;
}
}
// Configurar el observer
function setupObserver() {
console.log('[Content] Setting up observers');
const observer = new MutationObserver((mutations) => {
let shouldProcess = false;
for (const mutation of mutations) {
if (mutation.addedNodes.length > 0) {
shouldProcess = true;
break;
}
}
if (shouldProcess && !isProcessing) {
// console.log('[Content] Changes detected, processing...');
processLatexElements();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
console.log('[Content] All observers setup complete');
}
// Función de inicialización
async function initialize() {
console.log('[Content] Initializing...');
// Esperar a que la página esté completamente cargada
if (document.readyState !== 'complete') {
await new Promise(resolve => window.addEventListener('load', resolve));
}
// Verificar KaTeX
if (await checkKaTeX()) {
console.log('[Content] KaTeX available, setting up...');
setupObserver();
await processLatexElements();
} else {
console.error('[Content] KaTeX not available');
}
}
// Iniciar
initialize();