-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
27 lines (25 loc) · 843 Bytes
/
Copy pathbackground.js
File metadata and controls
27 lines (25 loc) · 843 Bytes
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
const connect = remotePort => {
let tabId = "unknown";
let origin = "unknown";
if (remotePort.sender && remotePort.sender.tab && remotePort.sender.url) {
tabId = remotePort.sender.tab.id;
const url = new URL(remotePort.sender.url);
origin = url.origin;
}
console.log("background connected", tabId, origin);
let count = 10;
remotePort.onMessage.addListener(data => {
console.log(`background onMessage (${count})`, tabId, origin, data);
count--;
if (!count) {
console.log("background disconnecting (0)", tabId, origin);
remotePort.disconnect();
}
});
remotePort.onDisconnect.addListener(data => {
console.log("background disconnected", tabId, origin, data.error, chrome.runtime.lastError);
});
};
chrome.runtime.onConnect.addListener(async (...args) => {
connect(...args);
});