Skip to content

Commit

Permalink
Chore: add allowlist on preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
situplastik committed Jan 11, 2021
1 parent 48c32da commit 225329c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
28 changes: 28 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const tabsWaitingToLoad = {};
const googleHostREs = [];
const youtubeHostREs = [];
const whitelistedHostREs = [];
const allowlistedHostREs = [];

async function isMACAddonEnabled () {
try {
Expand Down Expand Up @@ -171,11 +172,23 @@ function generateWhitelistedHostREs () {
}
}

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

async function loadExtensionSettings () {
extensionSettings = await browser.storage.sync.get();
if (extensionSettings.whitelist === undefined){
extensionSettings.whitelist = "";
}
if (extensionSettings.allowlist === undefined){
extensionSettings.allowlist = "";
}
}

async function clearGoogleCookies () {
Expand Down Expand Up @@ -288,6 +301,17 @@ function isWhitelistedURL (url) {
return false;
}

function isAllowlistedURL (url) {
generateAllowlistedHostREs();
const parsedUrl = new URL(url);
for (let allowlistedHostRE of allowlistedHostREs) {
if (allowlistedHostRE.test(parsedUrl.host)) {
return true;
}
}
return false;
}

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

if (!handleUrl && extensionSettings.allowlist.length!=0 && isAllowlistedURL(url)) {
handleUrl = true;
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ <h1>Settings</h1>
</label>
</p>

<p>
<label>
<textarea id="allowlist" value="" cols="80">
</textarea>
<br>
Allowlisted urls to use Google Container<br>
<em>(Use one url per line.)</em>
</label>
</p>

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

<hr>
Expand Down
30 changes: 16 additions & 14 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
function validate_whitelist() {
function validate_list(htmlId) {
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");
list_element = document.querySelector(htmlId);
if (list_element.value == "") {return [];}
list_domains = list_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.");
for (list_domain of list_domains) {
if (list_domain == "") {continue;}
if (list_domain.match(domain_regex)) {validated_domains.push(list_domain); continue;}
alert("'" + list_domain + "' is not a valid domain.");
return [];
}
return validated_domains;
}

function fill_whitelist_option(stored_whitelist) {
whitelist_text = (stored_whitelist === undefined) ? "" : stored_whitelist.join("\n");
document.querySelector("#whitelist").value = whitelist_text ? whitelist_text : "";
function fill_list_option(stored_list, idHtml) {
list_text = (stored_list === undefined) ? "" : stored_list.join("\n");
document.querySelector(idHtml).value = list_text ? list_text : "";
}


Expand All @@ -30,8 +30,9 @@ 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,
"whitelist": validate_whitelist()
"dont_override_containers": document.querySelector("#dont_override_containers").checked,
"whitelist": validate_list("#whitelist"),
"allowlist": validate_list("#allowlist")
});

browser.runtime.reload();
Expand All @@ -49,7 +50,8 @@ 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);
fill_list_option(res.whitelist, "#whitelist");
fill_list_option(res.allowlist, "#allowlist");
});
}

Expand Down

0 comments on commit 225329c

Please sign in to comment.