Skip to content

Commit c3ed560

Browse files
committed
improve notify
1 parent c138025 commit c3ed560

File tree

4 files changed

+103
-21
lines changed

4 files changed

+103
-21
lines changed

background.html

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<script src="common.js"></script>
99
<script src="off_handler.js"></script>
1010
<script src="ipaddr.js"></script>
11+
<script src="showNotify.js"></script>
1112
<script src="background.js"></script>
1213
<script src="menu.js"></script>
1314
</body>

g_background.js

-20
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,6 @@ async function onBeforeRequest(details)
112112

113113
}
114114

115-
async function showNotify(origin, target)
116-
{
117-
if ( ( await get_settings_local() ) ['notify'] === true)
118-
{
119-
chrome.notifications.create({
120-
"type": "basic",
121-
"iconUrl": 'icon.png',
122-
"title": `${origin} - ${addon_name}`,
123-
"message":
124-
`Blocked:
125-
126-
from page: ${origin}
127-
to fetch : ${target}` ,
128-
});
129-
}
130-
async function get_settings_local()
131-
{
132-
return ( await browser.storage.local.get() ) ;
133-
}
134-
}
135115

136116
function isLan(parsed_ip)
137117
{

options.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
<div>
1616
<input type="checkbox" id="checkbox-notify" >
17-
Notify when web wants to access private network
17+
Notify when web wants to access private network
18+
<span style="text-decoration: underline;">(need restart/reenable)</span>
1819
<!-- <span style="text-decoration: underline;">(need restart/reenable)</span> -->
1920
</input>
2021
</div>

showNotify.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
let notify_enabled = -1;
3+
4+
async function showNotify(origin, target)
5+
{
6+
if ( notify_enabled === -1) {
7+
notify_enabled = ( await get_settings_local() ) ['notify'] ;
8+
}
9+
10+
if (!notify_enabled===true)
11+
return;
12+
13+
newNotifyCome({origin, target});
14+
15+
async function get_settings_local()
16+
{
17+
return ( await browser.storage.local.get() ) ;
18+
}
19+
}
20+
21+
22+
function batchPopupSystemNotify(arr)
23+
{
24+
if (! arr.length > 0)
25+
return;
26+
27+
28+
const origin = arr[0].origin;
29+
const target = arr[0].target;
30+
31+
var notifyText = '';
32+
33+
notifyText =
34+
`Blocked:
35+
36+
from page: ${origin}
37+
to fetch : ${target}` ;
38+
39+
if (arr.length >= 2)
40+
notifyText += `\n\n... and other ${arr.length-1}`
41+
42+
popupSystemNotify(notifyText);
43+
}
44+
45+
function popupSystemNotify(notifyText)
46+
{
47+
chrome.notifications.create({
48+
"type": "basic",
49+
"iconUrl": 'icon.png',
50+
"title": `${addon_name}`,
51+
"message": notifyText,
52+
});
53+
}
54+
55+
let fifo = [];
56+
let fifo_locked = false;
57+
let timerId = null;
58+
59+
async function newNotifyCome(notification) {
60+
const origin = notification.origin;
61+
const target = notification.target;
62+
63+
function sleep(ms) {
64+
return new Promise(resolve => setTimeout(resolve, ms));
65+
}
66+
67+
for (var n=0; n<200;n++) {
68+
if (!fifo_locked) {
69+
fifo_locked = true;
70+
71+
if (timerId === null) {
72+
batchPopupSystemNotify( [ { origin, target } ] );
73+
timerId = setTimeout(onNotifyTimerout, 5000);
74+
} else {
75+
fifo.push(notification);
76+
}
77+
78+
fifo_locked = false;
79+
return;
80+
} else {
81+
await sleep(10);
82+
}
83+
}
84+
85+
console.warn("fifo_locked. Failed to wait until unlock");
86+
}
87+
88+
function onNotifyTimerout() {
89+
fifo_locked = true;
90+
91+
if (fifo.length > 0) {
92+
batchPopupSystemNotify(fifo);
93+
fifo = [];
94+
}
95+
96+
clearTimeout(timerId); // 清除定时器
97+
timerId = null;
98+
99+
fifo_locked = false;
100+
};

0 commit comments

Comments
 (0)