-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
40 lines (37 loc) · 1.12 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
// Handle keyboard commands
chrome.commands.onCommand.addListener(async (command) => {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab) {
console.error('No active tab found');
return;
}
// Verify content script is loaded, inject if needed
try {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => {
return window.__narrowerLoaded === true;
}
});
} catch (error) {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
});
}
chrome.tabs.sendMessage(tab.id, { action: command }, (response) => {
if (chrome.runtime.lastError) {
console.error('Error sending message:', chrome.runtime.lastError);
}
});
} catch (error) {
console.error('Command handling error:', error);
}
});
// Listen for content script ready message
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "contentScriptReady") {
sendResponse({ success: true });
}
});