Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to set multiple options to show instead of blocked … #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ function checkTab(id, isBeforeNav, isRepeat) {
let rollover = gOptions[`rollover${set}`];
let conjMode = gOptions[`conjMode${set}`];
let days = gOptions[`days${set}`];
let blockURL = gOptions[`blockURL${set}`];
let blockURLs = gOptions[`blockURLs${set}`];
let applyFilter = gOptions[`applyFilter${set}`];
let filterName = gOptions[`filterName${set}`];
let filterMute = gOptions[`filterMute${set}`];
Expand Down Expand Up @@ -605,7 +605,7 @@ function checkTab(id, isBeforeNav, isRepeat) {
log(`lockdown: ${lockdown}`);
log(`withinTimePeriods: ${withinTimePeriods}`);
log(`afterTimeLimit: ${afterTimeLimit}`);
log(`blockURL: ${blockURL}`);
log(`blockURLs: ${blockURLs}`);
if (blockRE) {
let res = blockRE.exec(pageURL);
if (res) {
Expand Down Expand Up @@ -644,11 +644,16 @@ function checkTab(id, isBeforeNav, isRepeat) {
} else {
gTabs[id].keyword = keyword;

let blockUrlsList = blockURLs.split("\n");
let positionOfBlockUrlToRedirectOn = Math.floor(Math.random() * (blockUrlsList.length));

let blockURL = blockUrlsList[positionOfBlockUrlToRedirectOn];

// Get final URL for block page
blockURL = getLocalizedURL(blockURL)
.replace(/\$K/g, keyword ? keyword : "")
.replace(/\$S/g, set)
.replace(/\$U/g, pageURLWithHash);
.replace(/\$K/g, keyword ? keyword : "")
.replace(/\$S/g, set)
.replace(/\$U/g, pageURLWithHash);

// Redirect page
browser.tabs.update(id, { url: blockURL });
Expand Down
10 changes: 7 additions & 3 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const PER_SET_OPTIONS = {
rollover: { type: "boolean", def: false, id: "rollover" },
conjMode: { type: "boolean", def: false, id: "conjMode" },
days: { type: "array", def: [false, true, true, true, true, true, false], id: "day" },
blockURL: { type: "string", def: DEFAULT_BLOCK_URL, id: "blockURL" },
blockURLs: { type: "string", def: DEFAULT_BLOCK_URL, id: "blockURLs" },
applyFilter: { type: "boolean", def: false, id: "applyFilter" },
filterName: { type: "string", def: "grayscale", id: "filterName" },
filterMute: { type: "boolean", def: false, id: "filterMute" },
Expand Down Expand Up @@ -316,8 +316,12 @@ function checkPosNegIntFormat(value) {

// Check blocking page URL format
//
function checkBlockURLFormat(url) {
return INTERNAL_BLOCK_URL.test(url) || getParsedURL(url).page;
function checkBlockURLsFormat(url) {
let listOfUrls = url.split("\n");
for(let i = 0; i < listOfUrls.length; ++i)
if(!INTERNAL_BLOCK_URL.test(listOfUrls[i]) && !getParsedURL(listOfUrls[i]).page)
return false;
return true;
}

// Convert times to minute periods
Expand Down
22 changes: 22 additions & 0 deletions options.css
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ body {
width: 408px;
}

.whatToShowInsteadOfBlocked {
display: flex;
padding-top: 7px;
padding-bottom: 7px;
}

.listOfSites {
float: left;
padding-right: 5%;
}

.predefinedOptionsList {
display: flex;
flex-direction: column;
}

.predefinedOption {
display: flex;
justify-content: space-between;
padding: 3px;
}

#form-container {
display: flex;
justify-content: center;
Expand Down
38 changes: 30 additions & 8 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,40 @@
<em>In this section, specify how you want to block these sites.</em>
</p>
<hr>

<p>
<label>Enter the fully specified URL of the page to show instead of these blocked sites:</label>
<div><input id="blockURL1" type="text" size="60" placeholder="http://somesite.com/somepage.html" title="Use $U to pass URL of blocked page"></div>
</p>
<p>
<div>
<label>Predefined URLs:</label>
<button id="defaultPage1" type="button">Default Page</button>
<button id="delayingPage1" type="button">Delaying Page</button>
<button id="blankPage1" type="button">Blank Page</button>
</div>
<label>(If you enter multiple pages each in new line, one of them will be every time randomly selected to show instead of these blocked sites)</label>
</p>
<div class="whatToShowInsteadOfBlocked">
<div class="listOfSites">
<textarea id="blockURLs1" cols="60" rows="10" spellcheck="false" placeholder="http://somesite.com/somepage.html" title="Use $U to pass URL of blocked page"></textarea>
</div>

<div class="predefinedOptionsList">
<label>Add predefined URLs:</label>

<div class="predefinedOption">
<button id="removeDefaultPage1" type="button">-</button>
<div>Default Page</div>
<button id="addDefaultPage1" type="button">+</button>
</div>
<div class="predefinedOption">
<button id="removeDelayingPage1" type="button">-</button>
<div>Delaying Page</div>
<button id="addDelayingPage1" type="button">+</button>
</div>
<div class="predefinedOption">
<button id="removeBlankPage1" type="button">-</button>
<div>Blank Page</div>
<button id="addBlankPage1" type="button">+</button>
</div>

</div>
</div>

<hr>
<p>
<input id="applyFilter1" type="checkbox">
Expand Down Expand Up @@ -533,7 +555,7 @@
<div id="alertBadMinutes" title="LeechBlock Options">
<p>Please enter the number of minutes in the correct format (as a positive whole number).</p>
</div>
<div id="alertBadBlockURL" title="LeechBlock Options">
<div id="alertBadBlockURLs" 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="alertBadNumSets" title="LeechBlock Options">
Expand Down
32 changes: 24 additions & 8 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ function initForm(numSets) {
});
$(`#setName${set}`).change(function (e) { updateBlockSetName(set, $(`#setName${set}`).val()); });
$(`#allDay${set}`).click(function (e) { $(`#times${set}`).val(ALL_DAY_TIMES); });
$(`#defaultPage${set}`).click(function (e) { $(`#blockURL${set}`).val(DEFAULT_BLOCK_URL); });
$(`#delayingPage${set}`).click(function (e) { $(`#blockURL${set}`).val(DELAYED_BLOCK_URL); });
$(`#blankPage${set}`).click(function (e) { $(`#blockURL${set}`).val("about:blank"); });
$(`#addDefaultPage${set}`).click(function (e) { addToListOfBlockUrls(DEFAULT_BLOCK_URL, set); });
$(`#addDelayingPage${set}`).click(function (e) { addToListOfBlockUrls(DELAYED_BLOCK_URL, set); });
$(`#addBlankPage${set}`).click(function (e) { addToListOfBlockUrls("about:blank", set); });
$(`#removeDefaultPage${set}`).click(function (e) { removeFromListOfBlockUrls(DEFAULT_BLOCK_URL, set); });
$(`#removeDelayingPage${set}`).click(function (e) { removeFromListOfBlockUrls(DELAYED_BLOCK_URL, set); });
$(`#removeBlankPage${set}`).click(function (e) { removeFromListOfBlockUrls("about:blank", set); });
$(`#resetOpts${set}`).click(function (e) {
resetSetOptions(set);
$("#alertResetOptions").dialog("open");
Expand Down Expand Up @@ -178,7 +181,7 @@ function saveOptions(event) {
let limitOffset = $(`#limitOffset${set}`).val();
let delaySecs = $(`#delaySecs${set}`).val();
let reloadSecs = $(`#reloadSecs${set}`).val();
let blockURL = $(`#blockURL${set}`).val();
let blockURLs = $(`#blockURLs${set}`).val();

// Check field values
if (!checkTimePeriodsFormat(times)) {
Expand Down Expand Up @@ -211,10 +214,10 @@ function saveOptions(event) {
$("#alertBadSeconds").dialog("open");
return false;
}
if (!checkBlockURLFormat(blockURL)) {
if (!checkBlockURLsFormat(blockURLs)) {
$("#tabs").tabs("option", "active", (set - 1));
$(`#blockURL${set}`).focus();
$("#alertBadBlockURL").dialog("open");
$(`#blockURLs${set}`).focus();
$("#alertBadBlockURLs").dialog("open");
return false;
}
}
Expand Down Expand Up @@ -996,7 +999,8 @@ function disableSetOptions(set, disabled) {
let items = [
"resetOpts",
"allDay",
"defaultPage", "delayingPage", "blankPage",
"addDefaultPage", "addDelayingPage", "addBlankPage",
"removeDefaultPage", "removeDelayingPage", "removeBlankPage",
"clearRegExpBlock", "genRegExpBlock",
"clearRegExpAllow", "genRegExpAllow",
"cancelLockdown"
Expand Down Expand Up @@ -1097,6 +1101,18 @@ function handleKeyDown(event) {
}
}

function removeFromListOfBlockUrls(urlToRemove, set) {
let listOfBlockUrls = $(`#blockURLs${set}`).val().split("\n");
let indexToRemove = listOfBlockUrls.indexOf(urlToRemove);
if(indexToRemove >= 0)
listOfBlockUrls.splice(indexToRemove, 1);
$(`#blockURLs${set}`).val(listOfBlockUrls.join("\n"));
}

function addToListOfBlockUrls(urlToAdd, set) {
$(`#blockURLs${set}`).val($(`#blockURLs${set}`).val() + "\n" + urlToAdd);
}

/*** STARTUP CODE BEGINS HERE ***/

browser.runtime.getPlatformInfo().then(
Expand Down