Skip to content

Commit

Permalink
Save lockdown options
Browse files Browse the repository at this point in the history
  • Loading branch information
proginosko committed Nov 26, 2017
1 parent b49b5ca commit 70085fb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
9 changes: 9 additions & 0 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ function cleanOptions(options) {
if (typeof options[`keywordRE${set}`] !== "string") {
options[`keywordRE${set}`] = "";
}
if (typeof options[`lockdown${set}`] !== "boolean") {
options[`lockdown${set}`] = false;
}

// Update legacy values
if (options[`blockURL${set}`] == LEGACY_DEFAULT_BLOCK_URL) {
Expand Down Expand Up @@ -110,6 +113,12 @@ function cleanOptions(options) {
if (typeof options["warnSecs"] !== "string") {
options["warnSecs"] = ""; // default: no warning
}
if (typeof options["lockdownHours"] !== "string") {
options["lockdownHours"] = ""; // default: blank
}
if (typeof options["lockdownMins"] !== "string") {
options["lockdownMins"] = ""; // default: blank
}
}

// Clean time data
Expand Down
10 changes: 5 additions & 5 deletions lockdown.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
<div>
<input id="hours" type="text" size="2" list="hours-list">
<datalist id="hours-list">
<option value="0">
<option value="1">
<option value="2">
<option value="3">
Expand All @@ -36,9 +35,9 @@
<option value="24">
</datalist>
<label for="hours">hour(s)</label>
&nbsp;&nbsp;&nbsp;&nbsp;
<input id="mins" type="text" size="2" list="mins-list">
<datalist id="mins-list">
<option value="0">
<option value="5">
<option value="10">
<option value="15">
Expand All @@ -57,9 +56,10 @@
<hr>
<p>
<label>Select the sites to block during the lockdown:</label><br>
<div id="sites">
<div id="blockSets">
<p>
<input id="site1" type="checkbox"><label id="siteLabel1" for="site1">Sites specified in Block Set 1</label>
<input id="blockSet1" type="checkbox">
<label id="blockSetLabel1" for="blockSet1">Sites specified in Block Set 1</label>
</p>
</div>
</p>
Expand All @@ -76,7 +76,7 @@

<div id="alerts" style="display: none;">
<div id="alertNoDuration" title="LeechBlock Lockdown">
<p>Please enter the duration of the lockdown.</p>
<p>Please enter a non-zero duration for the lockdown.</p>
</div>
<div id="alertNoSets" title="LeechBlock Lockdown">
<p>Please select the sites to lockdown.</p>
Expand Down
36 changes: 30 additions & 6 deletions lockdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,26 @@ function initializeForm() {
browser.storage.local.get().then(onGot, onError);

function onGot(options) {
let lockdownHours = options["lockdownHours"];
if (lockdownHours > 0) {
document.querySelector("#hours").value = lockdownHours;
}

let lockdownMins = options["lockdownMins"];
if (lockdownMins > 0) {
document.querySelector("#mins").value = lockdownMins;
}

for (let set = 1; set <= NUM_SETS; set++) {
let lockdown = options[`lockdown${set}`];
if (lockdown) {
document.querySelector(`#blockSet${set}`).checked = lockdown;
}

// Append custom set name to check box label (if specified)
let setName = options[`setName${set}`];
if (setName) {
document.querySelector(`#siteLabel${set}`).innerText += ` (${setName})`;
document.querySelector(`#blockSetLabel${set}`).innerText += ` (${setName})`;
}
}
}
Expand All @@ -37,7 +52,7 @@ function onActivate() {
let mins = document.querySelector("#mins").value;
let duration = hours * 3600 + mins * 60;

if (!duration) {
if (!duration || duration < 0) {
$("#alertNoDuration").dialog("open");
return;
}
Expand All @@ -48,7 +63,7 @@ function onActivate() {
// Request lockdown for each selected set
let noneSelected = true;
for (let set = 1; set <= NUM_SETS; set++) {
let selected = document.querySelector(`#site${set}`).checked;
let selected = document.querySelector(`#blockSet${set}`).checked;
if (selected) {
noneSelected = false;
let message = {
Expand All @@ -66,6 +81,15 @@ function onActivate() {
return;
}

// Save options for next time
let options = {};
options["lockdownHours"] = hours;
options["lockdownMins"] = mins;
for (let set = 1; set <= NUM_SETS; set++) {
options[`lockdown${set}`] = document.querySelector(`#blockSet${set}`).checked;
}
browser.storage.local.set(options);

// Request tab close
browser.runtime.sendMessage({ type: "close" });
}
Expand All @@ -82,12 +106,12 @@ function onCancel() {
/*** STARTUP CODE BEGINS HERE ***/

// Use HTML for first check box to create other check boxes
let siteHTML = $("#sites").html();
let blockSetHTML = $("#blockSets").html();
for (let set = 2; set <= NUM_SETS; set++) {
let nextSiteHTML = siteHTML
let nextSetHTML = blockSetHTML
.replace(/Block Set 1/g, `Block Set ${set}`)
.replace(/(id|for)="(\w+)1"/g, `$1="$2${set}"`);
$("#sites").append(nextSiteHTML);
$("#blockSets").append(nextSetHTML);
}

// Set up JQuery UI widgets
Expand Down

0 comments on commit 70085fb

Please sign in to comment.