-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbackground.js
49 lines (44 loc) · 1.66 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
const delay = (ms) => {
const startPoint = new Date().getTime()
while (new Date().getTime() - startPoint <= ms) {/* wait */}
}
function matchRuleShort(str, rule) {
return new RegExp(str.replace(/\*/g, "[^ ]*")).test(rule);
}
let handler;
chrome.storage.onChanged.addListener(function(changes, namespace) {
if (changes.hasOwnProperty('requestThrottler')) {
const throttlerConfig = changes.requestThrottler.newValue;
if (handler) {
chrome.webRequest.onBeforeRequest.removeListener(handler);
handler = null;
}
const urls = throttlerConfig.urls.filter((u) => !u.error && u.url !== '' && u.checked).map((u) => u.url.trim());
if (throttlerConfig.enabled && urls.length > 0) {
chrome.browserAction.setIcon({path: 'icon48-warning.png'});
handler = (info) => {
//ex: {checked: true, delay: '10000', error: false, url: 'https://stackoverflow.com/tags'}
const thisUrlConfig = throttlerConfig.urls.filter((item) => item.checked && matchRuleShort(item.url, info.url))[0];
if(thisUrlConfig && thisUrlConfig.checked) {
const thisUrlDelay = thisUrlConfig.delay || throttlerConfig.defaultDelay;
console.log(`URL Throttler: Intercepted ${info.url}, going to wait ${thisUrlDelay} ms...`);
delay(thisUrlDelay);
console.log('URL Throttler: Done');
}
return;
};
console.log('Blocking urls', urls);
chrome.webRequest.onBeforeRequest.addListener(
handler,
// filters
{
urls: urls,
},
// extraInfoSpec
["blocking"]
);
} else {
chrome.browserAction.setIcon({path: 'icon48.png'});
}
}
});