-
Notifications
You must be signed in to change notification settings - Fork 65
/
add-sites.js
120 lines (94 loc) · 2.54 KB
/
add-sites.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
/* 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); }
// Initialize form (with specified number of block sets)
//
function initForm() {
//log("initForm");
// Clear drop-down list of block sets
$("#blockSet").html("");
// Set up JQuery UI widgets
$("#addSites").button();
$("#addSites").click(onAddSites);
$("#cancel").button();
$("#cancel").click(onCancel);
}
// Refresh page
//
function refreshPage() {
//log("refreshPage");
$("#form").hide();
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);
let numSets = +options["numSets"];
// Initialize form
initForm();
setTheme(options["theme"]);
// Update drop-down list of block sets
let blockSetHTML = "";
for (let set = 1; set <= numSets; set++) {
blockSetHTML += `<option value="${set}">Block Set ${set}`;
let setName = options[`setName${set}`];
if (setName) {
blockSetHTML += ` (${setName})`;
}
blockSetHTML += "</option>";
}
$("#blockSet").html(blockSetHTML);
$("#form").show();
}
function onError(error) {
warn("Cannot get options: " + error);
$("#alertRetrieveError").dialog("open");
}
}
// Handle add button click
//
function onAddSites() {
//log("onAddSites");
let sites = cleanSites(getElement("sites").value);
let blockSet = getElement("blockSet").value;
let message = {
type: "add-sites",
sites: sites,
set: blockSet
};
// Request add sites for this set
browser.runtime.sendMessage(message);
$("#form").hide({ effect: "fade", complete: closePage });
}
// Handle cancel button click
//
function onCancel() {
//log("onCancel");
closePage();
}
// Close page
//
function closePage() {
// Request tab close
browser.runtime.sendMessage({ type: "close" });
}
/*** STARTUP CODE BEGINS HERE ***/
// Initialize alert dialogs
$("div[id^='alert']").dialog({
autoOpen: false,
modal: true,
width: 500,
buttons: {
OK: function () { $(this).dialog("close"); }
}
});
document.addEventListener("DOMContentLoaded", refreshPage);
document.addEventListener("focus", refreshPage);