Skip to content

Commit e132d2b

Browse files
committed
fix window not existing on chrome outside of tabs
1 parent ef2920e commit e132d2b

File tree

1 file changed

+128
-107
lines changed

1 file changed

+128
-107
lines changed

serviceWorker.js

+128-107
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,169 @@
1-
window.browser = (function () {
2-
return window.msBrowser ||
3-
window.browser ||
4-
window.chrome;
1+
const browserAPI = (() => {
2+
return self.msBrowser || self.browser || self.chrome;
53
})();
64

75
let ws = null;
86

9-
browser.runtime.onMessage.addListener(
10-
function (request, sender, sendResponse) {
11-
switch (request.type) {
12-
case "keep-alive":
13-
sendKeepAlive();
14-
break;
15-
case "cursor-update":
16-
sendCursorUpdate(request.x, request.y);
17-
break;
18-
case "logout":
19-
sendLogout();
20-
break;
21-
}
22-
}
23-
);
7+
browserAPI.runtime.onMessage.addListener(function (
8+
request,
9+
sender,
10+
sendResponse
11+
) {
12+
switch (request.type) {
13+
case "keep-alive":
14+
sendKeepAlive();
15+
break;
16+
case "cursor-update":
17+
sendCursorUpdate(request.x, request.y);
18+
break;
19+
case "logout":
20+
sendLogout();
21+
break;
22+
}
23+
});
2424

2525
//Catch URL changes
26-
browser.tabs.onActivated.addListener((activeInfo) => {
27-
browser.tabs.get(activeInfo.tabId, async (tab) => {
28-
console.log("[INFO] Active tab changed to: " + tab.url)
29-
let skinId = await getSkinIdFromStorage();
30-
sendLogin(tab.url, skinId);
31-
});
26+
browserAPI.tabs.onActivated.addListener((activeInfo) => {
27+
browserAPI.tabs.get(activeInfo.tabId, async (tab) => {
28+
console.log("[INFO] Active tab changed to: " + tab.url);
29+
let skinId = await getSkinIdFromStorage();
30+
sendLogin(tab.url, skinId);
31+
});
3232
});
3333

34-
browser.webNavigation.onCompleted.addListener(async (details) => {
35-
if (details.frameId === 0) {
36-
console.log("[INFO] Active tab url changed to:", details.url);
37-
let skinId = await getSkinIdFromStorage();
38-
sendLogin(details.url, skinId);
39-
}
34+
browserAPI.webNavigation.onCompleted.addListener(async (details) => {
35+
if (details.frameId === 0) {
36+
console.log("[INFO] Active tab url changed to:", details.url);
37+
let skinId = await getSkinIdFromStorage();
38+
sendLogin(details.url, skinId);
39+
}
4040
});
4141
//end-of catching URL changes
4242

43-
4443
//Outgoing traffic
4544
async function sendKeepAlive() {
46-
try {
47-
ws.send(JSON.stringify({ type: "keep-alive" }));
48-
console.log("[INFO] Sending a keep-alive!")
49-
} catch (error) {
50-
console.log("[ERROR] Tried sending a keep-alive but failed." + error)
51-
}
45+
try {
46+
ws.send(JSON.stringify({ type: "keep-alive" }));
47+
console.log("[INFO] Sending a keep-alive!");
48+
} catch (error) {
49+
console.log("[ERROR] Tried sending a keep-alive but failed." + error);
50+
}
5251
}
5352

5453
function sendCursorUpdate(x, y) {
55-
try {
56-
ws.send(JSON.stringify({ type: "cursor-update", x: x, y: y }));
57-
//console.log("[INFO] Sent a cursor-update: {x: " + x + " y: " + y + "}")
58-
} catch (error) {
59-
console.log("[ERROR] Tried sending a cursor-update but failed." + error)
60-
}
54+
try {
55+
ws.send(JSON.stringify({ type: "cursor-update", x: x, y: y }));
56+
//console.log("[INFO] Sent a cursor-update: {x: " + x + " y: " + y + "}")
57+
} catch (error) {
58+
console.log("[ERROR] Tried sending a cursor-update but failed." + error);
59+
}
6160
}
6261

6362
function sendLogin(url, skinId) {
64-
sendLogout();
65-
ws = new WebSocket("wss://alexinabox.de/wss/");
66-
67-
//Register onmessage event handler
68-
ws.onmessage = function (message) {
69-
//parse the message
70-
var data = JSON.parse(message.data);
71-
//if the message is a cursor update
72-
if (data.type == "cursor-update") {
73-
//update the cursor
74-
informUpdateCursor(data.id, data.x, data.y);
75-
}
76-
//if the message is a new client
77-
if (data.type == "connected") {
78-
//add the client to the list
79-
informAddClient(data.id, data.skinId || 0);
80-
}
81-
//if the message is a client disconnect
82-
if (data.type == "disconnected") {
83-
//remove the client from the list
84-
informRemoveClient(data.id);
85-
}
63+
sendLogout();
64+
ws = new WebSocket("wss://alexinabox.de/wss/");
65+
66+
//Register onmessage event handler
67+
ws.onmessage = function (message) {
68+
//parse the message
69+
var data = JSON.parse(message.data);
70+
//if the message is a cursor update
71+
if (data.type == "cursor-update") {
72+
//update the cursor
73+
informUpdateCursor(data.id, data.x, data.y);
8674
}
87-
88-
//when the websocket connection is established
89-
ws.onopen = function () {
90-
//send a message to the server
91-
ws.send(JSON.stringify({ type: "login", room: url, skinId: skinId || 0 }));
75+
//if the message is a new client
76+
if (data.type == "connected") {
77+
//add the client to the list
78+
informAddClient(data.id, data.skinId || 0);
9279
}
80+
//if the message is a client disconnect
81+
if (data.type == "disconnected") {
82+
//remove the client from the list
83+
informRemoveClient(data.id);
84+
}
85+
};
86+
87+
//when the websocket connection is established
88+
ws.onopen = function () {
89+
//send a message to the server
90+
ws.send(JSON.stringify({ type: "login", room: url, skinId: skinId || 0 }));
91+
};
9392
}
9493

9594
function sendLogout() {
96-
if (ws == null) {
97-
return;
98-
}
99-
ws.onmessage = null;
100-
ws.close();
101-
ws = null;
95+
if (ws == null) {
96+
return;
97+
}
98+
ws.onmessage = null;
99+
ws.close();
100+
ws = null;
102101
}
103102
//end-of outgoing traffic
104103

105-
106-
107104
//Incomming traffic
108105
async function informUpdateCursor(id, x, y) {
109-
const [tab] = await browser.tabs.query({ active: true, lastFocusedWindow: true });
110-
browser.tabs.sendMessage(tab.id, { type: "cursor-update", id: id, x: x, y: y })
111-
.catch((error) => {
112-
console.log("[ERROR] Failed to send cursor-update: " + error.message);
113-
});
114-
console.log("[INFO] Received cursor-update: { id: " + id + ", x: " + x + ", y: " + y + " }")
106+
const [tab] = await browserAPI.tabs.query({
107+
active: true,
108+
lastFocusedWindow: true,
109+
});
110+
browserAPI.tabs
111+
.sendMessage(tab.id, { type: "cursor-update", id: id, x: x, y: y })
112+
.catch((error) => {
113+
console.log("[ERROR] Failed to send cursor-update: " + error.message);
114+
});
115+
console.log(
116+
"[INFO] Received cursor-update: { id: " +
117+
id +
118+
", x: " +
119+
x +
120+
", y: " +
121+
y +
122+
" }"
123+
);
115124
}
116125

117126
async function informAddClient(id, skinId) {
118-
const [tab] = await browser.tabs.query({ active: true, lastFocusedWindow: true });
119-
browser.tabs.sendMessage(tab.id, { type: "add-client", id: id, skinId: skinId })
120-
.catch((error) => {
121-
console.log("[ERROR] Failed to send add-client: " + error.message);
122-
});
123-
console.log("[INFO] Received add-client: { id: " + id + ", skinId: " + skinId + " }")
127+
const [tab] = await browserAPI.tabs.query({
128+
active: true,
129+
lastFocusedWindow: true,
130+
});
131+
browserAPI.tabs
132+
.sendMessage(tab.id, { type: "add-client", id: id, skinId: skinId })
133+
.catch((error) => {
134+
console.log("[ERROR] Failed to send add-client: " + error.message);
135+
});
136+
console.log(
137+
"[INFO] Received add-client: { id: " + id + ", skinId: " + skinId + " }"
138+
);
124139
}
125140

126141
async function informRemoveClient(id) {
127-
const [tab] = await browser.tabs.query({ active: true, lastFocusedWindow: true });
128-
browser.tabs.sendMessage(tab.id, { type: "remove-client", id: id })
129-
.catch((error) => {
130-
console.log("[ERROR] Failed to send remove-client: " + error.message);
131-
});
132-
console.log("[INFO] Received remove-client: { id: " + id + " }")
142+
const [tab] = await browserAPI.tabs.query({
143+
active: true,
144+
lastFocusedWindow: true,
145+
});
146+
browserAPI.tabs
147+
.sendMessage(tab.id, { type: "remove-client", id: id })
148+
.catch((error) => {
149+
console.log("[ERROR] Failed to send remove-client: " + error.message);
150+
});
151+
console.log("[INFO] Received remove-client: { id: " + id + " }");
133152
}
134153
//end-of incomming traffic
135154

136155
//Storage
137156
async function getSkinIdFromStorage() {
138-
try {
139-
const result = await browser.storage.local.get("cursors.customization.skinId");
140-
const skinId = result["cursors.customization.skinId"] ?? 0; // Use nullish coalescing to default to 0
141-
console.log("[INFO] skinId is set to " + skinId);
142-
return skinId;
143-
} catch (error) {
144-
console.error("[ERROR] Failed to get skinId from storage:", error);
145-
return 0; // Default to 0 in case of error
146-
}
157+
try {
158+
const result = await browserAPI.storage.local.get(
159+
"cursors.customization.skinId"
160+
);
161+
const skinId = result["cursors.customization.skinId"] ?? 0; // Use nullish coalescing to default to 0
162+
console.log("[INFO] skinId is set to " + skinId);
163+
return skinId;
164+
} catch (error) {
165+
console.error("[ERROR] Failed to get skinId from storage:", error);
166+
return 0; // Default to 0 in case of error
167+
}
147168
}
148-
//end-of storage
169+
//end-of storage

0 commit comments

Comments
 (0)