-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceWorker.js
169 lines (155 loc) · 4.4 KB
/
serviceWorker.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const browserAPI = (() => {
return self.msBrowser || self.browser || self.chrome;
})();
let ws = null;
browserAPI.runtime.onMessage.addListener(function (
request,
sender,
sendResponse
) {
switch (request.type) {
case "keep-alive":
sendKeepAlive();
break;
case "cursor-update":
sendCursorUpdate(request.x, request.y);
break;
case "logout":
sendLogout();
break;
}
});
//Catch URL changes
browserAPI.tabs.onActivated.addListener((activeInfo) => {
browserAPI.tabs.get(activeInfo.tabId, async (tab) => {
console.log("[INFO] Active tab changed to: " + tab.url);
let skinId = await getSkinIdFromStorage();
sendLogin(tab.url, skinId);
});
});
browserAPI.webNavigation.onCompleted.addListener(async (details) => {
if (details.frameId === 0) {
console.log("[INFO] Active tab url changed to:", details.url);
let skinId = await getSkinIdFromStorage();
sendLogin(details.url, skinId);
}
});
//end-of catching URL changes
//Outgoing traffic
async function sendKeepAlive() {
try {
ws.send(JSON.stringify({ type: "keep-alive" }));
console.log("[INFO] Sending a keep-alive!");
} catch (error) {
console.log("[ERROR] Tried sending a keep-alive but failed." + error);
}
}
function sendCursorUpdate(x, y) {
try {
ws.send(JSON.stringify({ type: "cursor-update", x: x, y: y }));
//console.log("[INFO] Sent a cursor-update: {x: " + x + " y: " + y + "}")
} catch (error) {
console.log("[ERROR] Tried sending a cursor-update but failed." + error);
}
}
function sendLogin(url, skinId) {
sendLogout();
ws = new WebSocket("wss://alexinabox.de/wss/");
//Register onmessage event handler
ws.onmessage = function (message) {
//parse the message
var data = JSON.parse(message.data);
//if the message is a cursor update
if (data.type == "cursor-update") {
//update the cursor
informUpdateCursor(data.id, data.x, data.y);
}
//if the message is a new client
if (data.type == "connected") {
//add the client to the list
informAddClient(data.id, data.skinId || 0);
}
//if the message is a client disconnect
if (data.type == "disconnected") {
//remove the client from the list
informRemoveClient(data.id);
}
};
//when the websocket connection is established
ws.onopen = function () {
//send a message to the server
ws.send(JSON.stringify({ type: "login", room: url, skinId: skinId || 0 }));
};
}
function sendLogout() {
if (ws == null) {
return;
}
ws.onmessage = null;
ws.close();
ws = null;
}
//end-of outgoing traffic
//Incomming traffic
async function informUpdateCursor(id, x, y) {
const [tab] = await browserAPI.tabs.query({
active: true,
lastFocusedWindow: true,
});
browserAPI.tabs
.sendMessage(tab.id, { type: "cursor-update", id: id, x: x, y: y })
.catch((error) => {
console.log("[ERROR] Failed to send cursor-update: " + error.message);
});
console.log(
"[INFO] Received cursor-update: { id: " +
id +
", x: " +
x +
", y: " +
y +
" }"
);
}
async function informAddClient(id, skinId) {
const [tab] = await browserAPI.tabs.query({
active: true,
lastFocusedWindow: true,
});
browserAPI.tabs
.sendMessage(tab.id, { type: "add-client", id: id, skinId: skinId })
.catch((error) => {
console.log("[ERROR] Failed to send add-client: " + error.message);
});
console.log(
"[INFO] Received add-client: { id: " + id + ", skinId: " + skinId + " }"
);
}
async function informRemoveClient(id) {
const [tab] = await browserAPI.tabs.query({
active: true,
lastFocusedWindow: true,
});
browserAPI.tabs
.sendMessage(tab.id, { type: "remove-client", id: id })
.catch((error) => {
console.log("[ERROR] Failed to send remove-client: " + error.message);
});
console.log("[INFO] Received remove-client: { id: " + id + " }");
}
//end-of incomming traffic
//Storage
async function getSkinIdFromStorage() {
try {
const result = await browserAPI.storage.local.get(
"cursors.customization.skinId"
);
const skinId = result["cursors.customization.skinId"] ?? 0; // Use nullish coalescing to default to 0
console.log("[INFO] skinId is set to " + skinId);
return skinId;
} catch (error) {
console.error("[ERROR] Failed to get skinId from storage:", error);
return 0; // Default to 0 in case of error
}
}
//end-of storage