Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
50 lines (45 loc) · 1.71 KB
/
Copy pathbackground.js
File metadata and controls
50 lines (45 loc) · 1.71 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
// VISION – Background Service Worker v2.2
// On install → set defaults
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({
enabled: true,
fixedCount: 0,
totalFixed: 0,
theme: 'night'
});
});
// On startup (browser restart) → ensure enabled is always true by default
// This fires when Chrome starts and the extension wakes up
chrome.runtime.onStartup.addListener(() => {
chrome.storage.local.get(['enabled'], data => {
// If key is missing (first install before onInstalled fires), default to true
if (data.enabled === undefined) {
chrome.storage.local.set({ enabled: true, theme: 'night' });
}
});
});
// Badge update on tab switch
chrome.tabs.onActivated.addListener(async ({ tabId }) => {
try {
const data = await chrome.storage.local.get(['enabled']);
const on = data.enabled !== false;
chrome.action.setBadgeBackgroundColor({ color: on ? '#c0c0c0' : '#333' });
chrome.action.setBadgeText({ tabId, text: on ? 'ON' : '' });
} catch(e) {}
});
// Re-run on tab navigation (handles SPAs like Quora)
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status !== 'complete') return;
if (!tab.url) return;
if (tab.url.startsWith('chrome://') || tab.url.startsWith('chrome-extension://') ||
tab.url.startsWith('edge://') || tab.url.startsWith('about:')) return;
chrome.storage.local.get(['enabled'], data => {
if (data.enabled === false) return;
// Small delay to let page JS settle before we scan
setTimeout(() => {
chrome.tabs.sendMessage(tabId, { action: 'runNow' }, () => {
if (chrome.runtime.lastError) {} // content script may not be ready yet — ok
});
}, 800);
});
});