-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
executable file
·67 lines (54 loc) · 2.27 KB
/
main.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
chrome.runtime.onInstalled.addListener(function () {
console.log("Extension installed.");
chrome.runtime.openOptionsPage(function () {
console.log("Options page opened.");
});
});
// Browser action clicked
chrome.browserAction.onClicked.addListener(function () {
console.log("Browser action clicked.");
});
// Blocking
chrome.webRequest.onBeforeRequest.addListener(
function (requestDetails) {
const interceptedUrl = requestDetails.url;
const isProperToIntercept = requestDetails.method === "GET" && interceptedUrl.indexOf("http") === 0;
if (isProperToIntercept) {
console.log(JSON.stringify(requestDetails));
const options = {
period: localStorage.getItem("period") || 0,
activated: localStorage.getItem("activated") === "true",
lastInterceptingTime: localStorage.getItem("lastInterceptingTime") || 0
};
if (!options.activated) {
console.log('Reminder is not activated.');
return;
}
if (options.period < 1) {
console.log('Invalid period.');
return;
}
if (options.lastInterceptingTime === 0) {
localStorage.setItem("lastInterceptingTime", new Date().getTime());
console.log('Last intercepting time initiated.');
return;
}
const elapsedMillis = new Date().getTime() - options.lastInterceptingTime;
const periodInMillis = options.period * 60 * 1000;
console.log('Elapsed millis: ' + elapsedMillis + ' - ' + 'Period in millis: ' + periodInMillis);
if (options.period > 0 && elapsedMillis > periodInMillis) {
console.log('Intercepting for url: ' + interceptedUrl);
const url = chrome.extension.getURL("intercepting-page.html") + "?url=" + interceptedUrl;
localStorage.setItem("lastInterceptingTime", new Date().getTime());
return {
redirectUrl: url
};
}
}
},
{
urls: ["<all_urls>"],
types: ["main_frame"]
},
["blocking"]
);