-
Notifications
You must be signed in to change notification settings - Fork 66
/
blocked.js
137 lines (115 loc) · 3.66 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* 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/. */
var gBlockedURL;
// Processes info for blocking/delaying page
//
function processBlockInfo(info) {
if (!info) return;
gBlockedURL = info.blockedURL;
// Set theme
let link = document.getElementById("themeLink");
if (link) {
link.href = "/themes/" + (info.theme ? `${info.theme}.css` : "default.css");
}
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 && !info.disableLink) {
blockedURLLink.setAttribute("href", info.blockedURL);
}
let blockedSet = document.getElementById("lbBlockedSet");
if (info.blockedSet && blockedSet) {
if (info.blockedSetName) {
blockedSet.innerText = info.blockedSetName;
} else {
blockedSet.innerText += " " + info.blockedSet;
}
document.title += " (" + blockedSet.innerText + ")";
}
let keywordMatched = document.getElementById("lbKeywordMatched");
let keywordMatch = document.getElementById("lbKeywordMatch");
if (keywordMatched && keywordMatch) {
if (info.keywordMatch) {
keywordMatch.innerText = info.keywordMatch;
} else {
keywordMatched.style.display = "none";
}
}
let customMsgDiv = document.getElementById("lbCustomMsgDiv");
let customMsg = document.getElementById("lbCustomMsg");
if (customMsgDiv && customMsg) {
if (info.customMsg) {
customMsg.innerText = info.customMsg;
} else {
customMsgDiv.style.display = "none";
}
}
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,
delayCancel: info.delayCancel
};
countdown.interval = window.setInterval(onCountdownTimer, 1000, countdown);
}
if (info.reloadSecs) {
// Reload blocked page after specified time
window.setTimeout(reloadBlockedPage, info.reloadSecs * 1000);
}
}
// Handle countdown on delaying page
//
function onCountdownTimer(countdown) {
// Cancel countdown if document not focused
if (countdown.delayCancel && !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);
// Notify extension that delay countdown has completed
let message = {
type: "delayed",
blockedURL: countdown.blockedURL,
blockedSet: countdown.blockedSet
};
browser.runtime.sendMessage(message);
}
}
// Attempt to reload blocked page
//
function reloadBlockedPage() {
if (gBlockedURL) {
document.location.href = gBlockedURL;
}
}
// Request block info from extension
browser.runtime.sendMessage({ type: "blocked" }).then(processBlockInfo);