-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
47 lines (45 loc) · 1.8 KB
/
background.js
File metadata and controls
47 lines (45 loc) · 1.8 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
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'newData') {
// Store the data
chrome.storage.local.get({ storedData: [] }, (result) => {
const updatedData = result.storedData;
updatedData.push(message.data);
chrome.storage.local.set({ storedData: updatedData });
});
// Send to app window
chrome.storage.local.get('appWindowId', async (result) => {
if (result.appWindowId) {
const windows = await chrome.windows.getAll({ populate: true });
const appWindow = windows.find(w => w.id === result.appWindowId);
if (appWindow && appWindow.tabs.length > 0) {
chrome.scripting.executeScript({
target: { tabId: appWindow.tabs[0].id },
func: function(data) {
window.postMessage({
type: 'NEW_CHAT_DATA',
payload: data
}, '*');
},
args: [message.data]
});
}
}
});
}
});
// Handle window removals
chrome.windows.onRemoved.addListener((windowId) => {
chrome.storage.local.get(['broadcastWindowId', 'appWindowId', 'isMonitoring'], (result) => {
if (windowId === result.broadcastWindowId || windowId === result.appWindowId) {
chrome.storage.local.set({
isMonitoring: false,
broadcastWindowId: null,
appWindowId: null
});
chrome.runtime.sendMessage({
type: 'MONITORING_STOPPED',
reason: 'window_closed'
});
}
});
});