Skip to content

Commit

Permalink
Add option to whitelist domain from containment (#108)
Browse files Browse the repository at this point in the history
* Add option to whitelist domain from containment

* Use textarea for input of whitelisted domains

Before we used a simple text input, which wasn't able to contain
multiple lines
  • Loading branch information
sents committed Dec 9, 2020
1 parent 94d3ca0 commit 0989038
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
25 changes: 25 additions & 0 deletions background.js
Expand Up @@ -53,6 +53,7 @@ const canceledRequests = {};
const tabsWaitingToLoad = {};
const googleHostREs = [];
const youtubeHostREs = [];
const whitelistedHostREs = [];

async function isMACAddonEnabled () {
try {
Expand Down Expand Up @@ -161,6 +162,15 @@ function generateGoogleHostREs () {
}
}

function generateWhitelistedHostREs () {
if (whitelistedHostREs.length != 0) {return;}
const matchOperatorsRegex = /[|\\{}()[\]^$+*?.-]/g;
for (let whitelistedDomain of extensionSettings.whitelist) {
whitelistedDomain = whitelistedDomain.replace(matchOperatorsRegex, '\\$&');
whitelistedHostREs.push(new RegExp(`(^|\\.)${whitelistedDomain}$`));
}
}

async function loadExtensionSettings () {
extensionSettings = await browser.storage.sync.get();
}
Expand Down Expand Up @@ -264,6 +274,17 @@ function isYouTubeURL (url) {
return false;
}

function isWhitelistedURL (url) {
generateWhitelistedHostREs();
const parsedUrl = new URL(url);
for (let whitelistedHostRE of whitelistedHostREs) {
if (whitelistedHostRE.test(parsedUrl.host)) {
return true;
}
}
return false;
}

function isSearchPageURL (url) {
const parsedUrl = new URL(url);
return parsedUrl.pathname.startsWith('/search');
Expand Down Expand Up @@ -321,6 +342,10 @@ function shouldContainInto (url, tab) {
handleUrl = false;
}

if (handleUrl && extensionSettings.whitelist.length!=0 && isWhitelistedURL(url)) {
handleUrl = false;
}

if (handleUrl) {
if (tab.cookieStoreId !== googleCookieStoreId) {
if (tab.cookieStoreId !== "firefox-default" && extensionSettings.dont_override_containers) {
Expand Down
10 changes: 10 additions & 0 deletions options.html
Expand Up @@ -62,6 +62,16 @@ <h1>Settings</h1>
</label>
</p>

<p>
<label>
<textarea id="whitelist" value="" cols="80">
</textarea>
<br>
Whitelisted google urls<br>
<em>(Use one url per line.)</em>
</label>
</p>

<button type="submit">Save settings</button>

<hr>
Expand Down
25 changes: 24 additions & 1 deletion options.js
@@ -1,3 +1,24 @@
function validate_whitelist() {
domain_regex = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/;
whitelist_element = document.querySelector("#whitelist");
if (whitelist_element.value == "") {return [];}
whitelist_domains = whitelist_element.value.split("\n");
validated_domains = [];
for (whitelist_domain of whitelist_domains) {
if (whitelist_domain == "") {continue;}
if (whitelist_domain.match(domain_regex)) {validated_domains.push(whitelist_domain); continue;}
alert("'" + whitelist_domain + "' is not a valid domain.");
return [];
}
return validated_domains;
}

function fill_whitelist_option(stored_whitelist) {
whitelist_text = stored_whitelist.join("\n");
document.querySelector("#whitelist").value = whitelist_text ? whitelist_text : "";
}


function onOptionsPageSave(e)
{
e.preventDefault();
Expand All @@ -9,7 +30,8 @@ function onOptionsPageSave(e)
"ignore_prefpages": document.querySelector("#ignore_prefpages").checked,
"ignore_maps": document.querySelector("#ignore_maps").checked,
"ignore_flights": document.querySelector("#ignore_flights").checked,
"dont_override_containers": document.querySelector("#dont_override_containers").checked
"dont_override_containers": document.querySelector("#dont_override_containers").checked,
"whitelist": validate_whitelist()
});

browser.runtime.reload();
Expand All @@ -27,6 +49,7 @@ function onOptionsPageLoaded()
document.querySelector("#ignore_maps").checked = res.ignore_maps || false;
document.querySelector("#ignore_flights").checked = res.ignore_flights || false;
document.querySelector("#dont_override_containers").checked = res.dont_override_containers || false;
fill_whitelist_option(res.whitelist);
});
}

Expand Down

0 comments on commit 0989038

Please sign in to comment.