-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
87 lines (78 loc) · 2.7 KB
/
Copy pathbackground.js
File metadata and controls
87 lines (78 loc) · 2.7 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
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
// background.js
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.type === "NEXT_VERSION") {
const tabId = sender.tab.id;
if (msg.version) {
chrome.action.setIcon({
tabId, path: {
"16": "icons/icon-16-active.png",
"48": "icons/icon-48-active.png",
"128": "icons/icon-128-active.png"
}
});
} else {
chrome.action.setIcon({
tabId, path: {
"16": "icons/icon-16-disabled.png",
"48": "icons/icon-48-disabled.png",
"128": "icons/icon-128-disabled.png"
}
});
}
// Store the version for the popup to read
// We use an object key based on tabId to avoid collisions
// Using chrome.storage.local is persistent, but for tab-specific data,
// we need to be careful about cleanup.
// The user requested this specific implementation, so we follow it.
chrome.storage.local.set({ [tabId]: msg.version });
}
});
// Clean up storage when tab is closed
chrome.tabs.onRemoved.addListener((tabId) => {
chrome.storage.local.remove(tabId.toString());
});
// Reset state on navigation (ensure icon updates immediately)
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'loading') {
// Reset to disabled state immediately
chrome.action.setIcon({
tabId, path: {
"16": "icons/icon-16-disabled.png",
"48": "icons/icon-48-disabled.png",
"128": "icons/icon-128-disabled.png"
}
});
// Clear stored version
chrome.storage.local.remove(tabId.toString());
}
// Re-check on completion or URL change (handles SPA navigation)
if (changeInfo.status === 'complete' || changeInfo.url) {
chrome.tabs.sendMessage(tabId, { type: "CHECK_NEXT_JS" }).catch(() => {
// Ignore errors (e.g. content script not ready yet)
});
}
});
// Inject content script on install/update to support existing tabs
chrome.runtime.onInstalled.addListener(async () => {
const manifest = chrome.runtime.getManifest();
const contentScripts = manifest.content_scripts;
if (contentScripts) {
for (const cs of contentScripts) {
const tabs = await chrome.tabs.query({ url: cs.matches });
for (const tab of tabs) {
// Skip restricted pages
if (!tab.url || tab.url.startsWith('chrome://') || tab.url.startsWith('edge://') || tab.url.startsWith('about:') || tab.url.startsWith('chrome-extension://')) {
continue;
}
try {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: cs.js,
});
} catch (err) {
// Ignore errors (e.g. cannot access tab)
}
}
}
}
});