-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_worker.js
321 lines (297 loc) · 14 KB
/
service_worker.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//Edit this global variable to change the time it takes until a tab closed plays a sound.
//Enter in seconds
//Default is 36000, which equals 10 hours
//Does not affect the value the popup displays by default and resets to! Change seperately in popup.js
var defaultTabTimeOut = 36000;
//Edit this global variable to the sound file name (including file extension) of your desired default sound file
var defaultSoundFile = 'amongus_kill.ogg';
//Edit this global variable to change the language of the notification. Supported values: de, en
var defaultNotifLanguage = 'en';
//Edit this global variable to change the default state of notifications. true = on, false = off
var defaultNotifState = true;
//Every sound file in "/sounds" needs to be listed here for the randomize feature to include them.
var listOfFiles = ["custom", "de_m-1.ogg", "de_m-2.ogg", "uwu_hannah.ogg", "f_moan-1.ogg", "metal_pipe_falling.ogg", "samsung.ogg", "step_bro.ogg", "cant_believe.ogg", "rickroll.ogg", "vine_boom.ogg", "amongus_drip.ogg", "amongus_driplong.ogg", "amongus_kill.ogg", "amongus_roundstart.ogg", "amongus_kill_crewmate.ogg"];
//COMPATIBILITY AREA
//Functions that need to be edited/changed fundamentally to port to firefox
async function sendToOffscreen(type, data) {
// Create an offscreen document if one doesn't exist yet
console.log('du stinkst lool');
switch (type) {
case 'playsound-default':
playsoundDefault(data);
break;
default:
console.warn(`Unexpected message type received: '${type}'.`);
return false;
}
}
function playsoundDefault(data) {
const audio = new Audio(data.source);
audio.volume = data.volume;
audio.play();
}
//END COMPATIBILITY AREA
chrome.runtime.onInstalled.addListener(({ reason }) => {
if (reason === 'install') {
console.log("yay");
}
if (reason === 'update') {
console.log("New and improved!");
}
});
class AcidTab {
constructor(id, birthdate, url) {
this.id = id;
this.birthdate = birthdate;
this.url = url;
this.confirmed = false;
}
}
class SoundProperties {
constructor(source, volume) {
this.source = source;
this.volume = volume;
}
}
class navTab {
constructor(id, url) {
this.id = id;
this.url = url;
}
}
var currentCache = [];
var navCache = [];
var saveInProgress = false;
var navProcessInProgress = false;
async function saveCacheToStorage() {
if (!saveInProgress) { //checks if there is currently an instance of this function processing data
saveInProgress = true; //if not, enable check and begin processing
var tabs = await chrome.storage.local.get(["owotabs"]);
var openTabs = await chrome.tabs.query({});
let i;
for (i = 0; i < openTabs.length; i++) {
currentCache[currentCache.length] = new AcidTab(openTabs[i].id, Date.now(), openTabs[i].pendingUrl == null ? openTabs[i].url : openTabs[i].pendingUrl);
}
if (Object.keys(tabs).length === 0) { //check if owotabs key in storage is empty
tabs = []; //if yes, create new array to save in
console.log('array erstellt');
} else {
tabs = tabs['owotabs']; //eine ebene raus nehmen
isTabSavedYet(tabs);
}
while (currentCache.length > 0) {
tabs[tabs.length] = currentCache.splice(0, 1)[0]; //eine ebene aus cache splice raus nehmen
console.log('spliced 1 from cache to array');
}
chrome.storage.local.set({ "owotabs": tabs }).then(() => { //store cache variable in storage
console.log('stored array with new tabs in storage');
});
saveInProgress = false; //resets processing state boolean
}
}
async function processNavigation() {
if (!navProcessInProgress) {
var anythingChanged = false;
navProcessInProgress = true;
var tabs = await chrome.storage.local.get(["owotabs"]);
if (Object.keys(tabs).length === 0) { //check if owotabs key in storage is empty
tabs = []; //if yes, create new array to save in
console.log('array erstellt nav');
} else {
tabs = tabs['owotabs']; //eine ebene raus nehmen
let i;
let j;
for (i = navCache.length - 1; i >= 0; i--) {
console.log('starting check for navigation ' + i);
for (j = 0; j < tabs.length; j++) {
console.log('checking if nav id matches tabid ' + j);
if (navCache[i].id === tabs[j].id) {
if (!(navCache[i].url === tabs[j].url)) {
console.log('matches, old url: ' + tabs[j].url + ' new url: ' + navCache[i].url);
tabs[j].url = navCache.splice(i, 1)[0].url;
console.log('matches and url is new, updated');
anythingChanged = true;
} else {
navCache.splice(i, 1);
console.log('matches but url is the same');
}
break;
}
}
}
if (anythingChanged) {
chrome.storage.local.set({ "owotabs": tabs }).then(() => { //store cache variable in storage
console.log('stored array with updated urls in storage');
});
}
}
navProcessInProgress = false;
}
}
function isTabSavedYet(tabs) {
let i;
let j;
let k;
for (j = currentCache.length - 1; j >= 0; j--) {
console.log('starting check for current tab ' + j);
for (i = 0; i < tabs.length; i++) {
console.log('checking if currentCache tab ' + j + ' matches saved tab ' + i);
if (!tabs[i].confirmed) {
if (tabs[i].url === currentCache[j].url) {
//sachen machen wenn die url stimmt
tabs[i].id = currentCache.splice(j, 1)[0].id;
console.log('tab with url is saved, id updated');
tabs[i].confirmed = true;
break;
}
}
}
}
for (k = tabs.length - 1; k >= 0; k--) {
if (tabs[k].confirmed == false) {
tabs.splice(k, 1);
console.log('removed saved tab ' + k);
}
}
for (i = 0; i < tabs.length; i++) {
tabs[i].confirmed = false;
}
}
function newTab(tab) {
console.log(tab.id);
//print id of new tab
saveCacheToStorage();
//save tab id with creation time
}
async function tabClosed(tabId, removeInfo) {
if (!removeInfo.isWindowClosing) { //check that tab is not closing due to window being closed
var tabs = await chrome.storage.local.get(["owotabs"]);
let i;
var tabFound = false;
if (Object.keys(tabs).length === 0) { //check if owotabs key in storage is empty
console.log('array beim loeschen nicht vorhanden');
} else {
tabs = tabs['owotabs']; //eine ebene raus nehmen
console.log('array loaded');
var tabTimeOut = await chrome.storage.local.get(["nyatimeout"]);
if (Object.keys(tabTimeOut).length === 0) {
tabTimeOut = defaultTabTimeOut;
console.log('no timeout saved, set to default');
} else {
tabTimeOut = tabTimeOut['nyatimeout']; //eine ebene raus nehmen;
}
for (i = 0; i < tabs.length; i++) {
console.log('checking tab ' + i);
if (tabs[i].id === tabId) {
//sachen machen wenn die id stimmt
console.log('id matches');
if ((Date.now() - tabs[i].birthdate) > tabTimeOut * 1000) {
console.log('Yay, du hast einen alten Tab geschlossen');
var volume = await chrome.storage.local.get(["rawrvolume"]);
if (Object.keys(volume).length === 0) {
volume = 1.0; //set volume to 1.0 if not set yet
} else {
volume = volume['rawrvolume']; //eine ebene raus nehmen
}
if (volume != 0) {
console.log('die volume ist ' + volume);
var soundFileName = await chrome.storage.local.get(["uwusound"]);
var randomize = await chrome.storage.local.get(["grrandomize"]);
if (!(Object.keys(randomize).length === 0)) {
if (randomize["grrandomize"]) {
//randomize shit
soundFileName = listOfFiles[Math.floor(Math.random() * listOfFiles.length)];
} else {
if (Object.keys(soundFileName).length === 0) {
soundFileName = defaultSoundFile; //set soundFileName to default if not set yet
} else {
soundFileName = soundFileName['uwusound']; //eine ebene raus nehmen
}
}
} else {
if (Object.keys(soundFileName).length === 0) {
soundFileName = defaultSoundFile; //set soundFileName to default if not set yet
} else {
soundFileName = soundFileName['uwusound']; //eine ebene raus nehmen
}
}
if (soundFileName === 'custom') {
var customAudioBase64 = await chrome.storage.local.get(["owosound"]);
if (Object.keys(customAudioBase64).length === 0) {
sendToOffscreen('playsound-default', new SoundProperties('./sounds/' + defaultSoundFile, volume));
} else {
sendToOffscreen('playsound-default', new SoundProperties(customAudioBase64['owosound'], volume));
}
} else {
sendToOffscreen('playsound-default', new SoundProperties('./sounds/' + soundFileName, volume)); //play awesome sound
}
}
var notificationsEnabled = await chrome.storage.local.get(["ayaya"]);
if (Object.keys(notificationsEnabled).length === 0) {
notificationsEnabled = defaultNotifState;
} else {
notificationsEnabled = notificationsEnabled["ayaya"];
}
if (notificationsEnabled) {
var notifLanguage = await chrome.storage.local.get(["lelelelanguage"]);
if (Object.keys(notifLanguage).length === 0) {
notifLanguage = defaultNotifLanguage;
} else {
notifLanguage = notifLanguage["lelelelanguage"];
}
chrome.notifications.clear("ayaya");
switch (notifLanguage) {
case 'de':
chrome.notifications.create("ayaya", {
type: "basic",
iconUrl: "images/kill-512.png",
title: "Yay",
message: "Du hast einen alten Tab geschlossen!",
silent: true
});
break;
case 'en':
chrome.notifications.create("ayaya", {
type: "basic",
iconUrl: "images/kill-512.png",
title: "Yay",
message: "You closed an old tab!",
silent: true
});
break;
default:
chrome.notifications.create("ayaya", {
type: "basic",
iconUrl: "images/kill-512.png",
title: "SUS",
message: "AMOGUS????? U BREAK MA SHIT MEEEN!!!!11"
});
}
}
}
tabFound = true;
break;
}
}
}
//delete closed tab from array
if (tabFound === true) {
tabs.splice(i, 1);
chrome.storage.local.set({ "owotabs": tabs }).then(() => { //write array to storage
console.log('neues array ohne alten tab gespeichert');
});
} else {
console.log('tab not in array');
}
console.log(tabId); //print id of closed tab
}
}
function navigationCommitted(id, navdata) {
if (navdata.url) { //check if a navigation occured in the main tab
navCache[navCache.length] = new navTab(id, navdata.url); //save the new url and the tab id to cache
processNavigation(); //process cache
}
}
chrome.tabs.onCreated.addListener(newTab);
chrome.tabs.onRemoved.addListener(tabClosed);
chrome.tabs.onUpdated.addListener(navigationCommitted);