forked from proginosko/LeechBlockNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocked.js
92 lines (77 loc) · 2.51 KB
/
blocked.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Processes info for blocking/delaying page
//
function processBlockInfo(info) {
if (!info) return;
let blockedURL = document.getElementById("lbBlockedURL");
if (info.blockedURL && blockedURL) {
if (info.blockedURL.length > 60) {
blockedURL.innerText = info.blockedURL.substring(0, 57) + "...";
} else {
blockedURL.innerText = info.blockedURL;
}
}
let blockedURLLink = document.getElementById("lbBlockedURLLink");
if (info.blockedURL && blockedURLLink) {
blockedURLLink.setAttribute("href", info.blockedURL);
}
let blockedSet = document.getElementById("lbBlockedSet");
if (info.blockedSet && info.blockedSetName && blockedSet) {
if (info.blockedSetName) {
blockedSet.innerText = info.blockedSetName;
} else {
blockedSet.innerText += " " + info.blockedSet;
}
}
let unblockTime = document.getElementById("lbUnblockTime");
if (info.unblockTime && unblockTime) {
unblockTime.innerText = info.unblockTime;
}
let delaySecs = document.getElementById("lbDelaySeconds");
if (info.delaySecs && delaySecs) {
delaySecs.innerText = info.delaySecs;
// Start countdown timer
let countdown = {
blockedURL: info.blockedURL,
blockedSet: info.blockedSet,
delaySecs: info.delaySecs
};
countdown.interval = window.setInterval(onCountdownTimer, 1000, countdown);
}
}
// Handle countdown on delaying page
//
function onCountdownTimer(countdown) {
// Cancel countdown if document not focused
if (!document.hasFocus()) {
// Clear countdown timer
window.clearInterval(countdown.interval);
// Strike through countdown text
let countdownText = document.getElementById("lbCountdownText");
if (countdownText) {
countdownText.style.textDecoration = "line-through";
}
return;
}
countdown.delaySecs--;
// Update countdown seconds on page
let delaySecs = document.getElementById("lbDelaySeconds");
if (delaySecs) {
delaySecs.innerText = countdown.delaySecs;
}
if (countdown.delaySecs == 0) {
// Clear countdown timer
window.clearInterval(countdown.interval);
// Request extension allow blocked page and redirect
let message = {
type: "delayed",
blockedURL: countdown.blockedURL,
blockedSet: countdown.blockedSet
};
browser.runtime.sendMessage(message);
}
}
// Request block info from extension
browser.runtime.sendMessage({ type: "blocked" }).then(processBlockInfo);