Skip to content

Commit

Permalink
Add import options
Browse files Browse the repository at this point in the history
  • Loading branch information
proginosko committed Nov 12, 2017
1 parent f434526 commit ce54e6a
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 11 deletions.
30 changes: 30 additions & 0 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const NUM_SETS = 6;
const ALL_DAY_TIMES = "0000-2400";
const DEFAULT_BLOCK_URL = "blocked.html?$S&$U";
const DELAYED_BLOCK_URL = "delayed.html?$S&$U";
const LEGACY_DEFAULT_BLOCK_URL = "chrome://leechblock/content/blocked.xhtml?$S&$U";
const LEGACY_DELAYED_BLOCK_URL = "chrome://leechblock/content/delayed.xhtml?$S&$U";

const PARSE_URL = /^((([\w-]+):\/*(\w+(?::\w+)?@)?([\w-\.]+)(?::(\d*))?)([^\?#]*))(\?[^#]*)?(#.*)?$/;

Expand Down Expand Up @@ -76,6 +78,14 @@ function cleanOptions(options) {
if (typeof options[`keywordRE${set}`] !== "string") {
options[`keywordRE${set}`] = "";
}

// Update legacy values
if (options[`blockURL${set}`] == LEGACY_DEFAULT_BLOCK_URL) {
options[`blockURL${set}`] = DEFAULT_BLOCK_URL;
}
if (options[`blockURL${set}`] == LEGACY_DELAYED_BLOCK_URL) {
options[`blockURL${set}`] = DELAYED_BLOCK_URL;
}
}

// General options
Expand Down Expand Up @@ -365,3 +375,23 @@ function allTrue(array) {
return false;
}
}

// Encode day selection
//
function encodeDays(days) {
let dayCode = 0;
for (let i = 0; i < 7; i++) {
if (days[i]) dayCode |= (1 << i);
}
return dayCode;
}

// Decode day selection
//
function decodeDays(dayCode) {
let days = new Array(7);
for (let i = 0; i < 7; i++) {
days[i] = ((dayCode & (1 << i)) != 0);
}
return days;
}
11 changes: 11 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@
</select>
</p>
</fieldset>
<fieldset>
<legend>Export / Import</legend>
<p>
<button id="exportOptions" type="button">Export Options</button>
<button id="importOptions" type="button">Import Options</button>
<input type="file" id="importFile">
</p>
</fieldset>
</div>
</div>

Expand All @@ -208,6 +216,9 @@
<div id="alertBadBlockURL" title="LeechBlock Options">
<p>Please enter the URL for the blocking page in the correct format (as a fully specified URL).</p>
</div>
<div id="alertNoImportFile" title="LeechBlock Options">
<p>Please select a file containing the options to import.</p>
</div>
</div>

<script src="jquery-ui/external/jquery/jquery.js"></script>
Expand Down
192 changes: 181 additions & 11 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function retrieveOptions() {
let prevAddons = options[`prevAddons${set}`];
let prevConfig = options[`prevConfig${set}`];

// Append custom set name to panel heading (if specified)
// Apply custom set name to tab (if specified)
if (setName) {
document.querySelector(`#blockSetName${set}`).innerText = setName;
} else {
Expand Down Expand Up @@ -247,6 +247,176 @@ function retrieveOptions() {
}
}

// Export options
//
function exportOptions() {
}

// Import options
//
function importOptions() {
let file = document.querySelector("#importFile").files[0];
if (!file) {
$("#alertNoImportFile").dialog("open");
return;
}

// Read and process file
let reader = new FileReader();
reader.onload = processImportFile;
reader.readAsText(file);

function processImportFile(event) {
let text = event.target.result;
if (!text) {
warn("Cannot import options from file.");
return;
}

function isTrue(str) { return /^true$/i.test(str); }

// Extract options from text
let regexp = /^(\w+)=(.*)$/;
let lines = text.split(/[\n\r]+/);
let options = {};
for (let line of lines) {
let results = regexp.exec(line);
if (results) {
options[results[1]] = results[2];
}
}

for (let set = 1; set <= NUM_SETS; set++) {
// Get option values
let setName = options[`setName${set}`];
let sites = options[`sites${set}`];
let times = options[`times${set}`];
let limitMins = options[`limitMins${set}`];
let limitPeriod = options[`limitPeriod${set}`];
let conjMode = options[`conjMode${set}`];
let days = options[`days${set}`];
let blockURL = options[`blockURL${set}`];
let activeBlock = options[`activeBlock${set}`];
let countFocus = options[`countFocus${set}`];
let delayFirst = options[`delayFirst${set}`];
let delaySecs = options[`delaySecs${set}`];
let prevOpts = options[`prevOpts${set}`];
let prevAddons = options[`prevAddons${set}`];
let prevConfig = options[`prevConfig${set}`];

// Set component values
if (setName != undefined) {
let element = document.querySelector(`#setName${set}`);
if (!element.disabled) {
element.value = setName;
if (setName) {
document.querySelector(`#blockSetName${set}`).innerText = setName;
} else {
document.querySelector(`#blockSetName${set}`).innerText = `Block Set ${set}`;
}
}
}
if (sites != undefined) {
let element = document.querySelector(`#sites${set}`);
if (!element.disabled) {
element.value = sites.replace(/\s+/g, "\n");
}
}
if (times != undefined) {
let element = document.querySelector(`#times${set}`);
if (!element.disabled) {
element.value = times;
}
}
if (limitMins != undefined) {
let element = document.querySelector(`#limitMins${set}`);
if (!element.disabled) {
element.value = limitMins;
}
}
if (limitPeriod != undefined) {
let element = document.querySelector(`#limitPeriod${set}`);
if (!element.disabled) {
element.value = limitPeriod;
}
}
if (conjMode != undefined) {
let element = document.querySelector(`#conjMode${set}`);
if (!element.disabled) {
element.selectedIndex = isTrue(conjMode) ? 1 : 0;
}
}
if (days != undefined) {
days = decodeDays(days);
for (let i = 0; i < 7; i++) {
let element = document.querySelector(`#day${i}${set}`);
if (!element.disabled) {
element.checked = days[i];
}
}
}
if (blockURL != undefined) {
let element = document.querySelector(`#blockURL${set}`);
if (!element.disabled) {
element.value = blockURL;
}
}
if (activeBlock != undefined) {
let element = document.querySelector(`#activeBlock${set}`);
if (!element.disabled) {
element.checked = isTrue(activeBlock);
}
}
if (countFocus != undefined) {
let element = document.querySelector(`#countFocus${set}`);
if (!element.disabled) {
element.checked = isTrue(countFocus);
}
}
if (delayFirst != undefined) {
let element = document.querySelector(`#delayFirst${set}`);
if (!element.disabled) {
element.checked = isTrue(delayFirst);
}
}
if (delaySecs != undefined) {
let element = document.querySelector(`#delaySecs${set}`);
if (!element.disabled) {
element.value = delaySecs;
}
}
if (prevOpts != undefined) {
let element = document.querySelector(`#prevOpts${set}`);
if (!element.disabled) {
element.checked = isTrue(prevOpts);
}
}
if (prevAddons != undefined) {
let element = document.querySelector(`#prevAddons${set}`);
if (!element.disabled) {
element.checked = isTrue(prevAddons);
}
}
if (prevConfig != undefined) {
let element = document.querySelector(`#prevConfig${set}`);
if (!element.disabled) {
element.checked = isTrue(prevConfig);
}
}
}

// General options
let timerSize = options["timerSize"];
let timerLocation = options["timerLocation"];
if (timerSize != undefined) {
document.querySelector("#timerSize").value = timerSize;
}
if (timerLocation != undefined) {
document.querySelector("#timerLocation").value = timerLocation;
}
}
}

// Disable options for block set
//
function disableSetOptions(set) {
Expand Down Expand Up @@ -293,21 +463,21 @@ for (let set = 1; set <= NUM_SETS; set++) {
$(`#advOpts${set}`).css("display", "inline");
});
}
$("#exportOptions").click(exportOptions);
$("#importOptions").click(importOptions);
$("#saveOptions").button();
$("#saveOptions").click(saveOptions);
$("#saveOptionsClose").button();
$("#saveOptionsClose").click(saveOptionsClose);

let alerts = ["alertBadTimes", "alertBadTimeLimit", "alertBadSeconds", "alertBadBlockURL"];
for (let alert of alerts) {
$(`#${alert}`).dialog({
autoOpen: false,
modal: true,
buttons: {
OK: function() { $(this).dialog("close"); }
}
});
}
// Initialize alert dialogs
$("div[id^='alert']").dialog({
autoOpen: false,
modal: true,
buttons: {
OK: function() { $(this).dialog("close"); }
}
});

$("#form").show();

Expand Down

0 comments on commit ce54e6a

Please sign in to comment.