-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
52 lines (45 loc) · 1.66 KB
/
background.js
File metadata and controls
52 lines (45 loc) · 1.66 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
// background.js
chrome.runtime.onInstalled.addListener(() => {
console.log('Custom CSS Per Domain extension installed/updated.');
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
try {
const url = new URL(tab.url);
// Convert to lowercase to match what we do in popup.js:
const domain = url.host.toLowerCase();
console.log(`[BG] Tab updated: domain = ${domain}`);
chrome.storage.local.get(domain, (result) => {
if (chrome.runtime.lastError) {
console.error(`[BG] Storage error: ${chrome.runtime.lastError}`);
return;
}
const storedValue = result[domain];
if (!storedValue) {
console.log(`[BG] No data for ${domain}`);
return;
}
// We expect storedValue to be an array of { tag, css, enabled }
if (!Array.isArray(storedValue)) {
console.log(`[BG] Unexpected format for ${domain}. Skipping injection.`);
return;
}
// Inject each script that is enabled
storedValue.forEach((item) => {
if (item.enabled) {
const css = item.css || '';
if (css.trim()) {
console.log(`[BG] Injecting CSS for ${domain}, tag="${item.tag}"`);
chrome.scripting.insertCSS({
target: { tabId: tabId },
css: css,
});
}
}
});
});
} catch (err) {
console.error('[BG] Error parsing URL or injecting CSS:', err);
}
}
});