forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
54 lines (47 loc) · 1.62 KB
/
background.js
File metadata and controls
54 lines (47 loc) · 1.62 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
let gPeers = [];
// const REFRESH_INTERVAL = 30000; // 30 seconds
const REFRESH_INTERVAL = 3000; // 3 seconds
// Handle messages from the sidebar panel
browser.runtime.onMessage.addListener((message, sender) => {
switch (message.type) {
case "get-peers":
return Promise.resolve(gPeers);
default:
console.warn("Unknown message type:", message.type);
return Promise.reject(new Error("Unknown message type"));
}
});
async function fetchPeers() {
try {
const response = await fetch("http://localhost:8000/users", {});
gPeers = await response.json();
console.log("gPeers ", { gPeers });
// Notify all sidebar instances of the update
const tabs = await browser.tabs.query({});
for (const tab of tabs) {
try {
console.log('sending "peers-updated"');
browser.runtime.sendMessage({
type: "peers-updated",
peers: gPeers,
});
} catch (e) {
// Ignore errors from tabs that can't receive the message
console.debug("Could not send peers-updated to tab", tab.id, e);
}
}
} catch (error) {
// Suppress CORS errors in console
if (!error.message.includes("CORS")) {
console.error("Error fetching peers:", error);
}
// Keep the old peer list on error
}
}
// Initialize polling when the background script starts
fetchPeers(); // Initial fetch
setInterval(fetchPeers, REFRESH_INTERVAL);