-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathbackground.js
660 lines (573 loc) · 19.2 KB
/
background.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
* *************************************************************************************
*
* Copyright (C) 2024 FoE-Helper team - All Rights Reserved
* You may use, distribute and modify this code under the
* terms of the AGPL license.
*
* See file LICENSE.md or go to
* https://github.com/mainIine/foe-helfer-extension/blob/master/LICENSE.md
* for full license details.
*
* *************************************************************************************
*/
'use strict';
importScripts(
'vendor/browser-polyfill/browser-polyfill.min.js','vendor/dexie/dexie.min.js'
)
// @ts-ignore
let alertsDB = new Dexie("Alerts");
// Define Database Schema
alertsDB.version(1).stores({
alerts: "++id,[server+playerId],data.expires"
});
// separate code from global scope
{
/**
* removes an prefix from a string if present
* @param {string} str the string to remove the prefix from
* @param {string} prefix the prefix to remove
* @returns the string without the prefix
*/
function trimPrefix(str, prefix) {
if (str.startsWith(prefix)) {
return str.slice(prefix.length);
} else {
return str;
}
}
/**
* @typedef FoEAlertData
* @type {object}
* @property {string} title
* @property {string} body
* @property {number} expires
* @property {number} repeat
* @property {any[]|null} actions
* @property {string} category
* @property {boolean} persistent
* @property {string} tag
* @property {boolean} vibrate
*/
/**
* @typedef FoEAlert
* @type {object}
* @property {number} [id]
* @property {string} server
* @property {number} playerId
* @property {FoEAlertData} data
* @property {boolean} triggered
* @property {boolean} handled
* @property {boolean} hasNotification
* @property {boolean} delete
*/
const Alerts = (() => {
"use strict";
const db = alertsDB;
const prefix = 'foe-alert:';
const previevId = 'foe-alert-preview';
/**
* checks and limits the data for an alert
* @param {any} data
* @returns {FoEAlertData} a valid data object for alerts
*/
function getValidateAlertData(data) {
if (typeof data !== 'object') throw 'Alert: "data" needs to be an object';
// convert if possible
if (typeof data.expires === 'string') data.expires = Number.parseInt(data.expires);
if (data.expires === undefined && typeof data.datetime === 'string') data.expires = new Date(data.datetime).getTime();
if (typeof data.repeat === 'string') data.repeat = Number.parseInt(data.repeat);
if (data.category === undefined) data.category = '';
if (data.tag === undefined) data.tag = '';
if (data.vibrate === undefined) data.vibrate = false;
// todo: add string-length check
// check attribute types
if (typeof data.title !== 'string') throw 'Alert: "data.title" needs to be a string';
if (typeof data.body !== 'string') throw 'Alert: "data.body" needs to be a string';
if (!Number.isInteger(data.expires)) throw 'Alert: "data.expires" needs to be a integer';
if (!Number.isInteger(data.repeat)) throw 'Alert: "data.repeat" needs to be a integer';
if (data.actions != null && !(data.actions instanceof Array)) throw 'Alert: "data.actions" needs to be an array';
if (typeof data.category !== 'string') throw 'Alert: "data.category" needs to be a string';
if (typeof data.persistent !== 'boolean') throw 'Alert: "data.persistent" needs to be a boolean';
if (typeof data.tag !== 'string') throw 'Alert: "data.tag" needs to be a string';
if (typeof data.vibrate !== 'boolean') throw 'Alert: "data.vibrate" needs to be a boolean';
// copy attributes to prevent additional attributes
return {
title: data.title,
body: data.body,
expires: data.expires,
repeat: data.repeat,
actions: data.actions,
category: data.category,
persistent: data.persistent,
tag: data.tag,
vibrate: data.vibrate,
};
}
/**
* get alert by id
* @param {!number} id the id of the requested alert
* @returns {Promise<undefined|FoEAlert>}
*/
function getAlert(id) {
return db.alerts.get(id);
}
/**
* returns a Promise with all Alerts matching server and playerId if provided
* @param {{server: !string, playerId: !number}|null} filter the server and playerId to filter on
* @returns {Promise<FoEAlert[]>}
*/
function getAllAlerts(filter) {
if (filter == null) {
return db.alerts.toArray();
} else {
const {server, playerId} = filter;
return db.alerts.where({
server: server,
playerId: playerId
}).toArray();
}
}
/**
* creates a new alert dataset with no db entry and no triggering browser-alert
* @param {FoEAlertData} data the associated data
* @param {!string} server the associated origin
* @param {!number} playerId the associated playerId
* @returns {FoEAlert} the resulting alert dataset
*/
function createAlertData(data, server, playerId) {
return {
server: server,
playerId: playerId,
data: data,
triggered: false,
handled: false,
hasNotification: false,
delete: false,
};
}
/**
* creates a new alert
* @param {FoEAlertData} data the associated data
* @param {!string} server the associated origin
* @param {!number} playerId the associated playerId
* @returns {Promise<number>} the id number of the new alert
*/
async function createAlert(data, server, playerId) {
/** @type {FoEAlert} */
const alert = createAlertData(data, server, playerId);
return db.alerts
.add(alert)
.then((/** @type {!number} */id) => {
browser.alarms.create(
`foe-alert:${id}`,
{
when: data.expires
}
);
return id;
})
;
}
/**
* set alarm-data and reset alarm
* @param {number} id the id of the alarm to update
* @param {FoEAlertData} data the new associated data
*/
async function setAlertData(id, data) {
const tagId = prefix + id;
await Promise.all([
db.alerts.update(id, { data: data, triggered: false, handled: false }),
browser.alarms.clear(tagId),
]);
browser.alarms.create(
`foe-alert:${id}`,
{
when: data.expires
}
);
return id;
}
/**
* delets an alert
* @param {!number} id Alert-ID which should be deleted
* @returns {Promise<void>} Alarm removed
*/
async function deleteAlert(id) {
const tagId = prefix + id;
// delete alarm-trigger
const alarmClearP = browser.alarms.clear(tagId);
// don't actually delete an alarm with notification since the user can still interact with the notification
const notifications = await browser.notifications.getAll();
if (notifications[tagId]) {
// mark this alarm for deletion so it is deletet from the API point of view
await db.alerts.update(id, {delete: true});
} else {
await db.alerts.delete(id);
}
// make sure the alarm got cleared before finishing
await alarmClearP;
}
/**
* deletes all Alerts marked for deletion which don't have a notification displayed. // does not seem to work properly as future alerts are beeing deleted as well
*/
async function cleanupAlerts() {
const alerts = await getAllAlerts();
// don't actually delete an alarm with notification since the user can still interact with the notification
const notifications = await browser.notifications.getAll();
alerts.forEach(alert => {
const tagId = prefix + alert.id;
if (!notifications[tagId]) {
db.alerts.delete(alert.id);
}
});
}
/**
* triggers the notification for the given alert
* @param {FoEAlert} alert
* @returns {Promise<string>} the id of the new notification
*/
function triggerAlert(alert) {
return browser.notifications.create(
alert.id != null ? (prefix + alert.id) : previevId,
{
type: 'basic',
title: alert.data.title,
message: alert.data.body,
buttons: alert.data.actions,
requireInteraction: alert.data.persistent||false,
// @ts-ignore
contextMessage: 'FoE-Helper − '+trimPrefix(alert.server, "https://"),
iconUrl: '/images/app128.png',
eventTime: alert.data.expires
}
);
}
// Alarm triggered => show Notification
browser.alarms.onAlarm.addListener(async (alarm) => {
if (!alarm.name.startsWith(prefix)) return;
const alertId = Number.parseInt(alarm.name.substring(prefix.length));
if (!Number.isInteger(alertId) || alertId > Number.MAX_SAFE_INTEGER || alertId < 0) return;
const alertData = await db.transaction('rw', db.alerts, async () => {
const alertData = await Alerts.get(alertId);
if (alertData == null) return null;
alertData.triggered = true;
await db.alerts.put(alertData);
return alertData;
});
if (alertData == null) return;
triggerAlert(alertData);
});
// Notification clicked => search and open Webseite
browser.notifications.onClicked.addListener(async (notificationId) => {
if (!notificationId.startsWith(prefix)) return;
const alertId = Number.parseInt(notificationId.substring(prefix.length));
if (!Number.isInteger(alertId) || alertId > Number.MAX_SAFE_INTEGER || alertId < 0) return;
const alertData = await db.transaction('rw', db.alerts, async () => {
const alertData = await Alerts.get(alertId);
if (alertData == null) return null;
alertData.handled = true;
await db.alerts.put(alertData);
return alertData;
});
if (alertData == null) return;
const list = await browser.tabs.query({url: alertData.server+'/*'});
if (list.length > 0) {
const tab = list[0];
browser.tabs.update(tab.id, {active: true});
browser.windows.update(tab.windowId, {focused: true});
} else {
browser.tabs.create({url: alertData.server+'/game/index'});
}
});
browser.notifications.onClosed.addListener(async (notificationId) => {
if (!notificationId.startsWith(prefix)) return;
const alertId = Number.parseInt(notificationId.substring(prefix.length));
const alert = await getAlert(alertId);
if (alert) {
if (alert.delete) {
db.alerts.delete(alertId);
} else {
db.alerts.update(alertId, {handled: true});
}
}
});
// upon start cleanup alerts which didn't get removed properly.
//cleanupAlerts(); // deactivated - is triggered too often and deletes correct/active alarms (it seems the background.js is unloaded/reloaded regularly and this is triggered then unintentionally)
return {
getValidData: getValidateAlertData,
/**
* get Alert by id
* @param {!number} id the id of the requested alert
* @returns {Promise<undefined|FoEAlert>}
*/
get: async (id) => {
const alert = await getAlert(id);
return alert && !alert.delete ? alert : undefined;
},
/**
* returns a Promise with all Alerts matching server and playerId if provided
* @param {{server: !string, playerId: !number}|null} filter the server and playerId to filter on
* @returns {Promise<FoEAlert[]>}
*/
getAll: async (filter) => {
const alerts = await getAllAlerts(filter);
return alerts.filter(a => !a.delete);
},
delete: deleteAlert,
create: createAlert,
createTemp: createAlertData,
setData: setAlertData,
trigger: triggerAlert
};
})();
browser.runtime.onInstalled.addListener(() => {
"use strict";
//const version = browser.runtime.getManifest().version;
let lng = browser.i18n.getUILanguage();
// Fallback to "en"
if(lng !== 'de' && lng !== 'en'){
lng = 'en';
}
// @ts-ignore
//const askText = ask[lng];
if(!isDevMode() ) browser.tabs.create({
url: `https://foe-helper.com/extension/update?lang=${lng}`
});
});
/**
* Are we in DevMode?
*
* @returns {boolean}
*/
function isDevMode()
{
return !('update_url' in browser.runtime.getManifest());
}
const defaultInnoCDN = 'https://foede.innogamescdn.com/';
/**
* creates the return value for a successfull api-call
* @param {any} data the data to send as an response
* @returns {{ok: true, data: any}}
*/
function APIsuccess(data) {
return {ok: true, data: data};
}
/**
* creates the return value for an error
* @param {string} message the error message
* @returns {{ok: false, error: string}}
*/
function APIerror(message) {
return {ok: false, error: message};
}
/**
* handles internal and external extension communication
* @param {any} request
* @param {browser.runtime.MessageSender} sender
* @returns {Promise<{ok: true, data: any} | {ok: false, error: string}>}
*/
async function handleWebpageRequests(request, sender) {
"use strict";
if (!sender.origin) sender.origin = sender.url;
// remove sender.id if it was just a forwarded message, so it can't run into private API's
if (typeof request === 'object' && request.type === 'packed') {
delete sender.id;
request = request.data;
}
if (typeof request !== 'object') return APIerror('expecting an object as message');
if (typeof request.type !== 'string') return APIerror('expecting an "type": string');
/** @type {string} */
const type = request.type;
switch (type) {
case 'test': { // type
return APIsuccess({type: 'testresponse', data: request});
}
case 'alerts': { // type
// extended alerts-API for internal use
if (sender.id === browser.runtime.id) {
if (typeof request.action !== 'string') return APIerror('expecting an "action": string');
const action = request.action;
switch (action) {
case 'getAll': { // action
const alerts = await Alerts.getAll(null);
const strippedAlerts = alerts.map(a => ({
id: a.id,
data: a.data,
triggered: a.triggered,
handled: a.handled,
hasNotification: a.hasNotification,
}));
return APIsuccess(strippedAlerts);
}
case 'getAllRaw': { // action
const alerts = await Alerts.getAll(null);
return APIsuccess(alerts);
}
case 'setData': { // action
const id = request.id;
if (!Number.isInteger(id)) return APIerror('expecting an "id": integer');
const data = Alerts.getValidData(request.data);
const retId = await Alerts.setData(id, data);
return APIsuccess(retId);
}
case 'previewId': { // action
const id = request.id;
if (!Number.isInteger(id)) return APIerror('expecting an "id": integer');
const alert = await Alerts.get(id);
if (alert == null) return APIerror(`alert #${id} not found`);
// Deaktiviere die standard behandlung durch die entfernung der id
delete alert.id;
await Alerts.trigger(alert)
return APIsuccess(true);
}
case 'delete': { // action
const id = request.id;
if (!Number.isInteger(id)) return APIerror('expecting an "id": integer');
await Alerts.delete(id);
return APIsuccess(true);
}
} // end of switch action
} else { // limited alerts-API for external use
if (!Number.isInteger(request.playerId)) return APIerror('malformed request: expected "playerId": integer');
if (typeof request.action !== 'string') return APIerror('malformed request: expected "action": string');
const playerId = request.playerId;
const action = request.action;
// @ts-ignore
const server = sender.origin;
switch (action) {
case 'getAll': { // action
const alerts = await Alerts.getAll({server, playerId});
const strippedAlerts = alerts.map(a => (
{
id: a.id,
data: a.data,
triggered: a.triggered,
handled: a.handled,
hasNotification: a.hasNotification,
}
));
return APIsuccess(strippedAlerts);
}
case 'get': { // action
const id = request.id;
if (!Number.isInteger(id)) return APIerror('malformed request: expected "id": integer');
const alert = await Alerts.get(id);
if (alert == null || alert.server !== server || alert.playerId !== playerId) return APIsuccess(undefined);
const strippedAlert = {
id: alert.id,
data: alert.data,
triggered: alert.triggered,
handled: alert.handled,
hasNotification: alert.hasNotification,
};
return APIsuccess(strippedAlert);
}
case 'create': { // action
let data = null;
try {
data = Alerts.getValidData(request.data);
} catch (e) {
return APIerror(e);
}
const alertId = await Alerts.create(data, server, playerId);
return APIsuccess(alertId);
}
case 'setData': { // action
const id = request.id;
if (!Number.isInteger(id)) return APIerror('malformed request: expected "id": integer');
let data = null;
try {
data = Alerts.getValidData(request.data);
} catch (e) {
return APIerror(e);
}
const alert = await Alerts.get(id);
if (!alert || alert.server !== server || alert.playerId !== playerId) return APIsuccess(false);
await Alerts.setData(id, data);
return APIsuccess(true);
}
case 'preview': { // action
let data = null;
try {
data = Alerts.getValidData(request.data);
} catch (e) {
return APIerror(e);
}
const alert = Alerts.createTemp(data, server, playerId);
const id = await Alerts.trigger(alert);
setTimeout(() => {
browser.notifications.clear(id);
}, 5000);
return APIsuccess(true);
}
case 'delete': { // action
const id = request.id;
if (!Number.isInteger(id)) return APIerror('malformed request: expected "id": integer');
const alert = await Alerts.get(id);
if (!alert || alert.server !== server || alert.playerId !== playerId) return APIsuccess(false);
await Alerts.delete(id);
return APIsuccess(true);
}
} // end of switch action
} // end of limited alerts-API
} // end of alerts-API
case 'message': { // type
let t = request.time;
const opt = {
type: "basic",
title: request.title,
message: request.msg,
iconUrl: "images/app48.png"
};
// Compose desktop message
// @ts-ignore
await browser.notifications.create(null, opt).then(id => {
// Remove automatically after a defined timeout
setTimeout(()=> {browser.notifications.clear(id)}, t);
});
return APIsuccess(true);
}
case 'storeData': { // type
await browser.storage.local.set({ [request.key] : request.data });
return APIsuccess(true);
}
case 'send2Api': { // type
fetch(request.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: request.data
});
return APIsuccess(true);
}
case 'showNotification': { // type
try {
const title = request.title;
const options = request.options;
new Notification( title, {
actions: options.actions,
body: options.body,
dir: 'ltr',
icon: options.icon,
renotify: !!(options.tag),
requireInteraction: options.persistent,
vibrate: options.vibrate,
tag: options.tag,
});
}
catch( error ){
console.error('NotificationManager.notify: ', error );
console.log(request);
return APIsuccess(false);
}
return APIsuccess(true);
}
} // end of switch type
return APIerror(`unknown request type: ${type}`);
}
browser.runtime.onMessage.addListener(handleWebpageRequests);
browser.runtime.onMessageExternal.addListener(handleWebpageRequests);
// End of the separation from the global scope
}