-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
51 lines (46 loc) · 1.88 KB
/
background.js
File metadata and controls
51 lines (46 loc) · 1.88 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
// Background service worker for managing dynamic content scripts
// Note: Default domains (www.overleaf.com, cn.overleaf.com) are handled by static content_scripts in manifest.json
// This only manages additional custom domains added by users
// Initialize on installation
chrome.runtime.onInstalled.addListener(async (details) => {
if (details.reason === 'install' || details.reason === 'update') {
// Initialize storage with empty custom domains if not exists
const result = await chrome.storage.sync.get({ customDomains: [] });
// Register content scripts for custom domains only
await updateContentScripts(result.customDomains);
}
});
// Listen for storage changes to update content scripts
chrome.storage.onChanged.addListener(async (changes, areaName) => {
if (areaName === 'sync' && changes.customDomains) {
const customDomains = changes.customDomains.newValue || [];
await updateContentScripts(customDomains);
}
});
// Update content scripts with custom domains only
// Default domains are already handled by manifest.json
async function updateContentScripts(customDomains) {
try {
// Unregister all existing dynamic content scripts
try {
const existingScripts = await chrome.scripting.getRegisteredContentScripts();
if (existingScripts.length > 0) {
const ids = existingScripts.map(script => script.id);
await chrome.scripting.unregisterContentScripts({ ids });
}
} catch (error) {
// Ignore errors if no scripts are registered
}
// Only register if there are custom domains
if (customDomains && customDomains.length > 0) {
await chrome.scripting.registerContentScripts([{
id: "overleaf-vertical-custom",
matches: customDomains,
js: ["content.js"],
runAt: "document_idle"
}]);
}
} catch (error) {
console.error('Error updating content scripts:', error);
}
}