Skip to content

Commit

Permalink
Add "Load sites from URL" option
Browse files Browse the repository at this point in the history
  • Loading branch information
proginosko committed Dec 3, 2017
1 parent 8057f21 commit e8878f6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 7 deletions.
55 changes: 54 additions & 1 deletion background.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,67 @@ function retrieveOptions() {
gOptions = options;
gSetCounted = Array(NUM_SETS).fill(false);
refreshMenus();
loadSiteLists();
}

function onError(error) {
warn("Cannot get options: " + error);
}
}

// Load lists of sites if URLs specified
//
function loadSiteLists() {
//log("loadSiteLists");

let time = Date.now();

for (let set = 1; set <= NUM_SETS; set++) {
// Get sites for block set from HTTP source (if specified)
let sitesURL = gOptions[`sitesURL${set}`];
if (sitesURL) {
sitesURL = sitesURL.replace(/\$S/, set).replace(/\$T/, time);
try {
let req = new XMLHttpRequest();
req.set = set;
req.open("GET", sitesURL, true);
req.overrideMimeType("text/plain");
req.onload = onLoad;
req.send();
} catch (e) {
warn("Cannot load sites from URL: " + sitesURL);
}
}
}

function onLoad(event) {
let req = event.target;
if (req.readyState == XMLHttpRequest.DONE && req.status == 200) {
let set = req.set;
let sites = req.responseText;
sites = sites.replace(/\s+/g, " ").replace(/(^ +)|( +$)|(\w+:\/+)/g, "");
sites = sites.split(" ").sort().join(" "); // sort alphabetically

// Get regular expressions to match sites
let regexps = getRegExpSites(sites);

// Update options
gOptions[`sites${set}`] = sites;
gOptions[`blockRE${set}`] = regexps.blockRE;
gOptions[`allowRE${set}`] = regexps.allowRE;
gOptions[`keywordRE${set}`] = regexps.keywordRE;

// Save updated options to local storage
let options = {};
options[`sites${set}`] = sites;
options[`blockRE${set}`] = regexps.block;
options[`allowRE${set}`] = regexps.allow;
options[`keywordRE${set}`] = regexps.keyword;
browser.storage.local.set(options);
}
}
}

// Save time data to local storage
//
function saveTimeData() {
Expand All @@ -92,7 +146,6 @@ function saveTimeData() {
for (let set = 1; set <= NUM_SETS; set++) {
options[`timedata${set}`] = gOptions[`timedata${set}`];
}

browser.storage.local.set(options);
}

Expand Down
3 changes: 3 additions & 0 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ function cleanOptions(options) {
if (typeof options[`prevSupport${set}`] !== "boolean") {
options[`prevSupport${set}`] = false;
}
if (typeof options[`sitesURL${set}`] !== "string") {
options[`sitesURL${set}`] = "";
}
if (typeof options[`blockRE${set}`] !== "string") {
options[`blockRE${set}`] = "";
}
Expand Down
4 changes: 4 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"webNavigation"
],

"optional_permissions": [
"<all_urls>"
],

"background": {
"scripts": ["common.js", "background.js"]
},
Expand Down
9 changes: 7 additions & 2 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@
<p>
<button id="showAdvOpts1" type="button">Show Advanced Options</button>
</p>
<fieldset id="advOpts1" style="display: none;">
<fieldset id="advOpts1">
<legend>Advanced Options</legend>
<p>
<em>Be sure you understand the consequences of these options before selecting any of them!</em>
<em>Be sure you understand the consequences of these options before changing any of them!</em>
</p>
<hr>
<p>
Expand All @@ -156,6 +156,11 @@
<label for="prevSupport1">Prevent access to about:support at times when these sites are blocked</label>
</p>
<hr>
<p>
<label>Load list of sites from URL:</label>
<div><input id="sitesURL1" type="text" size="60" title="Use $S to pass block set number and $T to pass UTC timestamp in milliseconds"></div>
</p>
<hr>
<p>
<button id="cancelLockdown1" type="button" disabled="true">Cancel Active Lockdown</button>
</p>
Expand Down
28 changes: 24 additions & 4 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ function saveOptions() {
let sites = document.querySelector(`#sites${set}`).value;
sites = sites.replace(/\s+/g, " ").replace(/(^ +)|( +$)|(\w+:\/+)/g, "");
sites = sites.split(" ").sort().join(" "); // sort alphabetically
//let sitesURL = document.querySelector(`#sitesURL${set}`).value;
let times = document.querySelector(`#times${set}`).value;
let limitMins = document.querySelector(`#limitMins${set}`).value;
let limitPeriod = document.querySelector(`#limitPeriod${set}`).value;
Expand All @@ -80,6 +79,7 @@ function saveOptions() {
let prevOpts = document.querySelector(`#prevOpts${set}`).checked;
let prevAddons = document.querySelector(`#prevAddons${set}`).checked;
let prevSupport = document.querySelector(`#prevSupport${set}`).checked;
let sitesURL = document.querySelector(`#sitesURL${set}`).value;

// Get regular expressions to match sites
let regexps = getRegExpSites(sites);
Expand All @@ -100,9 +100,16 @@ function saveOptions() {
options[`prevOpts${set}`] = prevOpts;
options[`prevAddons${set}`] = prevAddons;
options[`prevSupport${set}`] = prevSupport;
options[`sitesURL${set}`] = sitesURL;
options[`blockRE${set}`] = regexps.block;
options[`allowRE${set}`] = regexps.allow;
options[`keywordRE${set}`] = regexps.keyword;

// Request permission to load sites from URL
if (sitesURL) {
let permissions = { origins: ["<all_urls>"] };
browser.permissions.request(permissions);
}
}

// General options
Expand Down Expand Up @@ -229,6 +236,7 @@ function retrieveOptions() {
let prevOpts = options[`prevOpts${set}`];
let prevAddons = options[`prevAddons${set}`];
let prevSupport = options[`prevSupport${set}`];
let sitesURL = options[`sitesURL${set}`];

// Apply custom set name to tab (if specified)
if (setName) {
Expand All @@ -255,6 +263,7 @@ function retrieveOptions() {
document.querySelector(`#prevOpts${set}`).checked = prevOpts;
document.querySelector(`#prevAddons${set}`).checked = prevAddons;
document.querySelector(`#prevSupport${set}`).checked = prevSupport;
document.querySelector(`#sitesURL${set}`).value = sitesURL;
}

// General options
Expand Down Expand Up @@ -350,6 +359,7 @@ function exportOptions() {
let prevOpts = document.querySelector(`#prevOpts${set}`).checked;
let prevAddons = document.querySelector(`#prevAddons${set}`).checked;
let prevSupport = document.querySelector(`#prevSupport${set}`).checked;
let sitesURL = document.querySelector(`#sitesURL${set}`).value;

// Set option values
options[`setName${set}`] = setName;
Expand All @@ -367,6 +377,7 @@ function exportOptions() {
options[`prevOpts${set}`] = prevOpts;
options[`prevAddons${set}`] = prevAddons;
options[`prevSupport${set}`] = prevSupport;
options[`sitesURL${set}`] = sitesURL;
}

// General options
Expand Down Expand Up @@ -441,6 +452,7 @@ function importOptions() {
let prevOpts = options[`prevOpts${set}`];
let prevAddons = options[`prevAddons${set}`];
let prevSupport = options[`prevSupport${set}`];
let sitesURL = options[`sitesURL${set}`];

// Set component values
if (setName != undefined) {
Expand Down Expand Up @@ -541,6 +553,12 @@ function importOptions() {
element.checked = isTrue(prevSupport);
}
}
if (sitesURL != undefined) {
let element = document.querySelector(`#sitesURL${set}`);
if (!element.disabled) {
element.value = sitesURL;
}
}
}

// General options
Expand Down Expand Up @@ -579,11 +597,12 @@ function importOptions() {
//
function disableSetOptions(set) {
let items = [
"setName", "sites", "times", "allDay", "limitMins", "limitPeriod", "conjMode",
"setName", "sites",
"times", "allDay", "limitMins", "limitPeriod", "conjMode",
"day0", "day1", "day2", "day3", "day4", "day5", "day6",
"blockURL", "defaultPage", "delayingPage", "blankPage", "homePage",
"activeBlock", "countFocus", "delayFirst", "delaySecs",
"prevOpts", "prevAddons", "prevSupport", "cancelLockdown"
"prevOpts", "prevAddons", "prevSupport", "sitesURL", "cancelLockdown"
];
for (let item of items) {
let element = document.querySelector(`#${item}${set}`);
Expand Down Expand Up @@ -659,13 +678,14 @@ for (let set = 1; set <= NUM_SETS; set++) {
$(`#homePage${set}`).click(function (e) { $(`#blockURL${set}`).val("about:home"); });
$(`#showAdvOpts${set}`).click(function (e) {
$(`#showAdvOpts${set}`).css("display", "none");
$(`#advOpts${set}`).css("display", "inline");
$(`#advOpts${set}`).css("display", "block");
});
$(`#cancelLockdown${set}`).click(function (e) {
browser.runtime.sendMessage({ type: "lockdown", set: set });
this.disabled = true;
$("#alertLockdownCancel").dialog("open");
});
$(`#advOpts${set}`).css("display", "none");
}
$("#exportOptions").click(exportOptions);
$("#importOptions").click(importOptions);
Expand Down

0 comments on commit e8878f6

Please sign in to comment.