Repository navigation
Expand file tree
/
Copy pathblocked.js
More file actions
186 lines (159 loc) · 4.97 KB
/
Copy pathblocked.js
File metadata and controls
186 lines (159 loc) · 4.97 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* 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/. */
const browser = chrome;
var gBlockedURL;
var gBlockedSet;
var gHashCode;
// Create 32-bit integer hash code from string
//
function hashCode32(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) - hash + str.charCodeAt(i)) | 0;
}
return hash;
}
// Processes info for blocking page
//
function processBlockInfo(info) {
if (!info) return;
gBlockedURL = info.blockedURL;
gBlockedSet = info.blockedSet;
gHashCode = info.password ? hashCode32(info.password) : 0;
// Set theme
let themeLink = document.getElementById("themeLink");
if (themeLink) {
themeLink.href = "/themes/" + (info.theme ? `${info.theme}.css` : "default.css");
}
// Set custom style
let customStyle = document.getElementById("customStyle");
if (customStyle) {
customStyle.innerText = info.customStyle;
}
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;
keywordMatched.style.display = "";
} else {
keywordMatched.style.display = "none";
}
}
let passwordInput = document.getElementById("lbPasswordInput");
let passwordSubmit = document.getElementById("lbPasswordSubmit");
if (passwordInput && passwordSubmit) {
passwordInput.focus();
passwordSubmit.onclick = onSubmitPassword;
}
let customMsgDiv = document.getElementById("lbCustomMsgDiv");
let customMsg = document.getElementById("lbCustomMsg");
if (customMsgDiv && customMsg) {
if (info.customMsg) {
customMsg.innerText = info.customMsg;
customMsgDiv.style.display = "";
} 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 = {
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: gBlockedURL,
blockedSet: gBlockedSet
};
browser.runtime.sendMessage(message);
}
}
// Handle submit button on password page
//
function onSubmitPassword() {
let passwordInput = document.getElementById("lbPasswordInput");
if (hashCode32(passwordInput.value) == gHashCode) {
// Notify extension that password was successfully entered
let message = {
type: "password",
blockedURL: gBlockedURL,
blockedSet: gBlockedSet
};
browser.runtime.sendMessage(message);
} else {
// Clear input field and flash background
passwordInput.value = "";
passwordInput.classList.add("error");
window.setTimeout(() => { passwordInput.classList.remove("error"); }, 400);
}
}
// 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);