-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
59 lines (51 loc) · 1.89 KB
/
background.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
console.log('[Background] Service worker starting...');
// Cargar KaTeX
async function loadKaTeX() {
try {
const url = chrome.runtime.getURL('katex/katex.min.js');
const response = await fetch(url);
const text = await response.text();
return new Function(text)();
} catch (error) {
console.error('[Background] Error loading KaTeX:', error);
return false;
}
}
// Estado global
let katexInstance = null;
// Manejar mensajes desde el content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('[Background] Message received:', request);
if (request.type === 'CHECK_KATEX') {
const available = typeof katex !== 'undefined';
console.log('[Background] KaTeX check:', available);
sendResponse({ available });
return true;
}
if (request.type === 'RENDER_LATEX') {
if (typeof katex === 'undefined') {
console.error('[Background] KaTeX not available');
sendResponse({ success: false, error: 'KaTeX not available' });
return true;
}
try {
console.log('[Background] Attempting to render:', request.text);
const rendered = katex.renderToString(request.text, {
throwOnError: false,
displayMode: request.text.includes("\\["),
output: 'html'
});
console.log('[Background] Render successful');
sendResponse({ success: true, rendered });
} catch (error) {
console.error('[Background] Render error:', error);
sendResponse({ success: false, error: error.message });
}
return true;
}
});
// Inicializar
loadKaTeX().then(() => {
console.log('[Background] KaTeX loaded:', typeof katex !== 'undefined');
});
console.log('[Background] Service worker initialized');