Skip to content

Commit

Permalink
Add options to use regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
proginosko committed Jan 1, 2018
1 parent beac220 commit 454c8fe
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
8 changes: 4 additions & 4 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ function checkTab(id, url, isRepeat) {

for (let set = 1; set <= NUM_SETS; set++) {
// Get regular expressions for matching sites to block/allow
let blockRE = gOptions[`blockRE${set}`];
let blockRE = gOptions[`regexpBlock${set}`] || gOptions[`blockRE${set}`];
if (!blockRE) continue; // no block for this set
let allowRE = gOptions[`allowRE${set}`];
let allowRE = gOptions[`regexpAllow${set}`] || gOptions[`allowRE${set}`];
let keywordRE = gOptions[`keywordRE${set}`];

// Get options for preventing access to about:addons and about:support
Expand Down Expand Up @@ -508,9 +508,9 @@ function updateTimeData(url, secsOpen, secsFocus) {

for (let set = 1; set <= NUM_SETS; set++) {
// Get regular expressions for matching sites to block/allow
let blockRE = gOptions[`blockRE${set}`];
let blockRE = gOptions[`regexpBlock${set}`] || gOptions[`blockRE${set}`];
if (!blockRE) continue; // no block for this set
let allowRE = gOptions[`allowRE${set}`];
let allowRE = gOptions[`regexpAllow${set}`] || gOptions[`allowRE${set}`];

// Test URL against block/allow regular expressions
if (testURL(pageURL, blockRE, allowRE)) {
Expand Down
6 changes: 6 additions & 0 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ function cleanOptions(options) {
if (typeof options[`sitesURL${set}`] !== "string") {
options[`sitesURL${set}`] = "";
}
if (typeof options[`regexpBlock${set}`] !== "string") {
options[`regexpBlock${set}`] = "";
}
if (typeof options[`regexpAllow${set}`] !== "string") {
options[`regexpAllow${set}`] = "";
}
if (typeof options[`blockRE${set}`] !== "string") {
options[`blockRE${set}`] = "";
}
Expand Down
18 changes: 18 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@
<div><input id="sitesURL1" type="text" size="60" title="Use $S to pass block set number and $T to pass UTC timestamp in milliseconds">
&nbsp;&nbsp;<a href="http://www.proginosko.com/leechblock/faq/load-from-url/" target="_blank">What does this do?</a></div>
</p>
<p>
<label>Use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions" target="_blank">regular expression</a>
to match URLs to <strong>block</strong> (overrides list of sites):</label>
<div>
<input id="regexpBlock1" type="text" size="60">
<button id="clearRegExpBlock1" type="button">Clear</button>
<button id="genRegExpBlock1" type="button">Generate From Site List</button>
</div>
</p>
<p>
<label>Use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions" target="_blank">regular expression</a>
to match URLs to <strong>allow</strong> (overrides exceptions in list of sites):</label>
<div>
<input id="regexpAllow1" type="text" size="60">
<button id="clearRegExpAllow1" type="button">Clear</button>
<button id="genRegExpAllow1" type="button">Generate From Site List</button>
</div>
</p>
<hr>
<p>
<button id="cancelLockdown1" type="button" disabled="true">Cancel Active Lockdown</button>
Expand Down
41 changes: 40 additions & 1 deletion options.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ function saveOptions() {
let prevAddons = getElement(`prevAddons${set}`).checked;
let prevSupport = getElement(`prevSupport${set}`).checked;
let sitesURL = getElement(`sitesURL${set}`).value;
let regexpBlock = getElement(`regexpBlock${set}`).value;
let regexpAllow = getElement(`regexpAllow${set}`).value;

// Get regular expressions to match sites
let regexps = getRegExpSites(sites);
Expand All @@ -108,6 +110,8 @@ function saveOptions() {
options[`prevAddons${set}`] = prevAddons;
options[`prevSupport${set}`] = prevSupport;
options[`sitesURL${set}`] = sitesURL;
options[`regexpBlock${set}`] = regexpBlock;
options[`regexpAllow${set}`] = regexpAllow;
options[`blockRE${set}`] = regexps.block;
options[`allowRE${set}`] = regexps.allow;
options[`keywordRE${set}`] = regexps.keyword;
Expand Down Expand Up @@ -250,6 +254,8 @@ function retrieveOptions() {
let prevAddons = options[`prevAddons${set}`];
let prevSupport = options[`prevSupport${set}`];
let sitesURL = options[`sitesURL${set}`];
let regexpBlock = options[`regexpBlock${set}`];
let regexpAllow = options[`regexpAllow${set}`];

// Apply custom set name to tab (if specified)
if (setName) {
Expand Down Expand Up @@ -277,6 +283,8 @@ function retrieveOptions() {
getElement(`prevAddons${set}`).checked = prevAddons;
getElement(`prevSupport${set}`).checked = prevSupport;
getElement(`sitesURL${set}`).value = sitesURL;
getElement(`regexpBlock${set}`).value = regexpBlock;
getElement(`regexpAllow${set}`).value = regexpAllow;
}

// General options
Expand Down Expand Up @@ -375,6 +383,8 @@ function exportOptions() {
let prevAddons = getElement(`prevAddons${set}`).checked;
let prevSupport = getElement(`prevSupport${set}`).checked;
let sitesURL = getElement(`sitesURL${set}`).value;
let regexpBlock = getElement(`regexpBlock${set}`).value;
let regexpAllow = getElement(`regexpAllow${set}`).value;

// Set option values
options[`setName${set}`] = setName;
Expand All @@ -393,6 +403,8 @@ function exportOptions() {
options[`prevAddons${set}`] = prevAddons;
options[`prevSupport${set}`] = prevSupport;
options[`sitesURL${set}`] = sitesURL;
options[`regexpBlock${set}`] = regexpBlock;
options[`regexpAllow${set}`] = regexpAllow;
}

// General options
Expand Down Expand Up @@ -476,6 +488,8 @@ function importOptions() {
let prevAddons = options[`prevAddons${set}`];
let prevSupport = options[`prevSupport${set}`];
let sitesURL = options[`sitesURL${set}`];
let regexpBlock = options[`regexpBlock${set}`];
let regexpAllow = options[`regexpAllow${set}`];

// Set component values
if (setName != undefined) {
Expand Down Expand Up @@ -582,6 +596,18 @@ function importOptions() {
element.value = sitesURL;
}
}
if (regexpBlock != undefined) {
let element = getElement(`regexpBlock${set}`);
if (!element.disabled) {
element.value = regexpBlock;
}
}
if (regexpAllow != undefined) {
let element = getElement(`regexpAllow${set}`);
if (!element.disabled) {
element.value = regexpAllow;
}
}
}

// General options
Expand Down Expand Up @@ -631,7 +657,10 @@ function disableSetOptions(set) {
"day0", "day1", "day2", "day3", "day4", "day5", "day6",
"blockURL", "defaultPage", "delayingPage", "blankPage", "homePage",
"activeBlock", "countFocus", "delayFirst", "delaySecs",
"prevOpts", "prevAddons", "prevSupport", "sitesURL", "cancelLockdown"
"prevOpts", "prevAddons", "prevSupport", "sitesURL",
"regexpBlock", "clearRegExpBlock", "genRegExpBlock",
"regexpAllow", "clearRegExpAllow", "genRegExpAllow",
"cancelLockdown"
];
for (let item of items) {
let element = getElement(`${item}${set}`);
Expand Down Expand Up @@ -709,6 +738,16 @@ for (let set = 1; set <= NUM_SETS; set++) {
$(`#showAdvOpts${set}`).css("display", "none");
$(`#advOpts${set}`).css("display", "block");
});
$(`#clearRegExpBlock${set}`).click(function (e) { $(`#regexpBlock${set}`).val(""); });
$(`#genRegExpBlock${set}`).click(function (e) {
let sites = $(`#sites${set}`).val();
$(`#regexpBlock${set}`).val(getRegExpSites(sites).block);
});
$(`#clearRegExpAllow${set}`).click(function (e) { $(`#regexpAllow${set}`).val(""); });
$(`#genRegExpAllow${set}`).click(function (e) {
let sites = $(`#sites${set}`).val();
$(`#regexpAllow${set}`).val(getRegExpSites(sites).allow);
});
$(`#cancelLockdown${set}`).click(function (e) {
browser.runtime.sendMessage({ type: "lockdown", set: set });
this.disabled = true;
Expand Down

0 comments on commit 454c8fe

Please sign in to comment.