forked from proginosko/LeechBlockNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
override.js
211 lines (183 loc) · 5.17 KB
/
override.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* 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/. */
function log(message) { console.log("[LBNG] " + message); }
function warn(message) { console.warn("[LBNG] " + message); }
function getElement(id) { return document.getElementById(id); }
var gAccessConfirmed = false;
var gAccessRequiredInput;
var gClockOffset;
var gOverrideConfirm;
var gOverrideMins;
// Initialize page
//
function initializePage() {
//log("initializePage");
browser.storage.local.get("sync").then(onGotSync, onError);
function onGotSync(options) {
if (options["sync"]) {
browser.storage.sync.get().then(onGot, onError);
} else {
browser.storage.local.get().then(onGot, onError);
}
}
function onGot(options) {
cleanOptions(options);
setTheme(options["theme"]);
gClockOffset = options["clockOffset"];
gOverrideConfirm = options["orc"];
gOverrideMins = options["orm"];
if (!gOverrideMins) {
$("#alertNoOverride").dialog("open");
return;
}
confirmAccess(options);
}
function onError(error) {
warn("Cannot get options: " + error);
$("#alertRetrieveError").dialog("open");
}
}
// Close page
//
function closePage() {
// Request tab close
browser.runtime.sendMessage({ type: "close" });
}
// Confirm access to override
//
function confirmAccess(options) {
let ora = options["ora"];
let password = options["password"];
let hpp = options["hpp"];
if (ora == 1 && password) {
gAccessRequiredInput = password;
if (hpp) {
$("#promptPasswordInput").attr("type", "password");
} else {
$("#promptPasswordInput").attr("type", "text");
}
$("#promptPasswordInput").val("");
$("#promptPassword").dialog("open");
$("#promptPasswordInput").focus();
} else if (ora > 1) {
let code = createAccessCode(32);
if (ora > 2) {
code += createAccessCode(32);
}
if (ora > 3) {
code += createAccessCode(64);
}
gAccessRequiredInput = code;
displayAccessCode(code, options["accessCodeImage"]);
$("#promptAccessCodeInput").val("");
$("#promptAccessCode").dialog("open");
$("#promptAccessCodeInput").focus();
} else {
activateOverride();
}
}
// Display access code (as text or image)
//
function displayAccessCode(code, asImage) {
let codeText = getElement("promptAccessCodeText");
let codeImage = getElement("promptAccessCodeImage");
let codeCanvas = getElement("promptAccessCodeCanvas");
if (asImage) {
// Display code as image
codeText.style.display = "none";
codeImage.style.display = "";
let ctx = codeCanvas.getContext("2d");
ctx.font = "normal 14px monospace";
codeCanvas.width = ctx.measureText(code.substring(0, 64)).width + 8;
codeCanvas.height = (code.length == 128) ? 40 : 24;
ctx.font = "normal 14px monospace"; // resizing canvas resets font!
ctx.fillStyle = "#000";
if (code.length == 128) {
ctx.fillText(code.substring(0, 64), 4, 16);
ctx.fillText(code.substring(64), 4, 32);
} else {
ctx.fillText(code, 4, 16);
}
} else {
// Display code as text
codeText.style.display = "";
codeImage.style.display = "none";
if (code.length == 128) {
codeText.appendChild(document.createTextNode(code.substring(0, 64)));
codeText.appendChild(document.createElement("br"));
codeText.appendChild(document.createTextNode(code.substring(64)));
} else {
codeText.appendChild(document.createTextNode(code));
}
}
}
// Activate override
//
function activateOverride() {
// Request override
browser.runtime.sendMessage({ type: "override" });
if (gOverrideConfirm) {
// Calculate end time
let endTime = new Date(Date.now() + (gClockOffset * 60000) + (gOverrideMins * 60000));
// Show confirmation dialog
$("#alertOverrideEndTime").html(endTime.toLocaleTimeString());
$("#alertOverrideActivated").dialog("open");
} else {
// Close page immediately (no confirmation dialog)
closePage();
}
}
// Initialize access control prompt
//
function initAccessControlPrompt(prompt) {
// Create functions for buttons
let dialogButtons = {
OK: function () {
let input = $(`#${prompt}Input`);
if (input.val() == gAccessRequiredInput) {
gAccessConfirmed = true;
activateOverride();
$(`#${prompt}`).dialog("close");
} else {
input.val("");
input.effect({ effect: "highlight", color: "#ff0000" });
input.focus();
}
},
Cancel: function () {
$(`#${prompt}`).dialog("close");
}
};
// Initialize prompt dialog
$(`#${prompt}`).dialog({
autoOpen: false,
modal: true,
width: 600,
buttons: dialogButtons,
close: function (event, ui) { if (!gAccessConfirmed) closePage(); }
});
// Connect ENTER key to OK button
$(`#${prompt}Input`).keydown(
function (event) {
if (event.which == 13) {
dialogButtons.OK();
}
}
);
}
/*** STARTUP CODE BEGINS HERE ***/
// Initialize alert dialogs
$("div[id^='alert']").dialog({
autoOpen: false,
modal: true,
width: 500,
buttons: {
OK: function () { $(this).dialog("close"); }
},
close: function (event, ui) { closePage(); }
});
// Initialize access control prompts
initAccessControlPrompt("promptPassword");
initAccessControlPrompt("promptAccessCode");
document.addEventListener("DOMContentLoaded", initializePage);