-
Notifications
You must be signed in to change notification settings - Fork 2
/
showNotify.js
100 lines (74 loc) · 2.05 KB
/
showNotify.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
let notify_enabled = -1;
async function showNotify(origin, target)
{
if ( notify_enabled === -1) {
notify_enabled = ( await get_settings_local() ) ['notify'] ;
}
if (!notify_enabled===true)
return;
newNotifyCome({origin, target});
async function get_settings_local()
{
return ( await browser.storage.local.get() ) ;
}
}
function batchPopupSystemNotify(arr)
{
if (! arr.length > 0)
return;
const origin = arr[0].origin;
const target = arr[0].target;
var notifyText = '';
notifyText =
`Blocked:
from page: ${origin}
to fetch : ${target}` ;
if (arr.length >= 2)
notifyText += `\n\n... and other ${arr.length-1}`
popupSystemNotify(notifyText);
}
function popupSystemNotify(notifyText)
{
chrome.notifications.create({
"type": "basic",
"iconUrl": 'icon.png',
"title": `${addon_name}`,
"message": notifyText,
});
}
let fifo = [];
let fifo_locked = false;
let timerId = null;
async function newNotifyCome(notification) {
const origin = notification.origin;
const target = notification.target;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
for (var n=0; n<200;n++) {
if (!fifo_locked) {
fifo_locked = true;
if (timerId === null) {
batchPopupSystemNotify( [ { origin, target } ] );
timerId = setTimeout(onNotifyTimerout, 5000);
} else {
fifo.push(notification);
}
fifo_locked = false;
return;
} else {
await sleep(10);
}
}
console.warn("fifo_locked. Failed to wait until unlock");
}
function onNotifyTimerout() {
fifo_locked = true;
if (fifo.length > 0) {
batchPopupSystemNotify(fifo);
fifo = [];
}
clearTimeout(timerId); // 清除定时器
timerId = null;
fifo_locked = false;
};