-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
78 lines (71 loc) · 2.38 KB
/
Copy pathbackground.js
File metadata and controls
78 lines (71 loc) · 2.38 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
let isEnabled = false;
let activeTabId = null;
let isProcessing = false;
// Update extension icon
function updateIcon() {
const iconPath = isEnabled ? 'icons/icon48.png' : 'icons/icon48.png';
chrome.action.setIcon({ path: iconPath });
}
// Listen for tab activation
chrome.tabs.onActivated.addListener((activeInfo) => {
activeTabId = activeInfo.tabId;
if (isEnabled) {
// Send message to the newly activated tab
chrome.tabs.sendMessage(activeTabId, {
action: 'tabActive',
active: true
}).catch(() => {
// If content script is not loaded, inject it
chrome.scripting.executeScript({
target: { tabId: activeTabId },
files: ['content.js']
}).then(() => {
// Send message after injection
setTimeout(() => {
chrome.tabs.sendMessage(activeTabId, {
action: 'tabActive',
active: true
});
}, 100);
});
});
}
});
// Listen for tab updates (for local files)
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url &&
(tab.url.startsWith('file://') || tab.url.startsWith('http://') || tab.url.startsWith('https://'))) {
activeTabId = tabId;
// Inject content script if needed
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['content.js']
}).catch(() => {
// Content script might already be injected
});
if (isEnabled) {
setTimeout(() => {
chrome.tabs.sendMessage(tabId, {
action: 'tabActive',
active: true
});
}, 100);
}
}
});
// Listen for messages from content scripts
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'getTabState') {
sendResponse({
active: sender.tab.id === activeTabId,
enabled: isEnabled
});
} else if (request.action === 'toggleOutlines') {
isEnabled = request.enabled;
activeTabId = sender.tab.id;
updateIcon();
}
return true; // Keep the message channel open for async response
});
// Initialize icon state
updateIcon();