-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
29 lines (26 loc) · 920 Bytes
/
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
// Track which tabs have had the content script injected
const injectedTabs = new Set();
// Listen for tab updates
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url && !tab.url.startsWith('chrome://')) {
// Check if we've already injected into this tab
if (!injectedTabs.has(tabId)) {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['content.js']
})
.then(() => {
injectedTabs.add(tabId);
})
.catch(err => console.log('Script injection failed:', err));
}
}
});
// Clean up when tabs are closed
chrome.tabs.onRemoved.addListener((tabId) => {
injectedTabs.delete(tabId);
});
// Handle tab replacement (e.g., when a page is loaded from the back/forward cache)
chrome.tabs.onReplaced.addListener((addedTabId, removedTabId) => {
injectedTabs.delete(removedTabId);
});