-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
62 lines (56 loc) · 1.68 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
let connections = new Set();
chrome.runtime.onConnect.addListener(function(port) {
if (port.name === 'sidepanel') {
connections.add(port);
port.onDisconnect.addListener(function() {
connections.delete(port);
});
}
});
chrome.sidePanel
.setPanelBehavior({ openPanelOnActionClick: true })
.catch((error) => console.error('Error setting panel behavior:', error));
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === 'SELECTED_TEXT') {
for (let port of connections) {
port.postMessage({
type: 'ADD_TEXT_BUBBLE',
text: request.text
});
}
sendResponse({ status: 'success' });
}
return true;
});
// Check if content script needs injection
async function checkContentScript(tabId) {
try {
await chrome.tabs.sendMessage(tabId, { type: 'CHECK_CONNECTION' });
return true;
} catch {
return false;
}
}
// Inject content script if needed
async function injectContentScript(tabId) {
try {
await chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['content.js']
});
return true;
} catch (error) {
console.error('Content script injection failed:', error);
return false;
}
}
// Periodic content script check
setInterval(async () => {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (tabs[0]) {
const hasContentScript = await checkContentScript(tabs[0].id);
if (!hasContentScript) {
await injectContentScript(tabs[0].id);
}
}
}, 60000);