Skip to content

Commit

Permalink
Merge pull request #114 from situplastik/master
Browse files Browse the repository at this point in the history
Chore: add allowlist on preferences
  • Loading branch information
hackerncoder authored Mar 3, 2021
2 parents bfd505e + 5c0d656 commit 5fb66dd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
34 changes: 29 additions & 5 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 @@ -319,7 +343,11 @@ function shouldContainInto (url, tab) {
return false;
}

let handleUrl = isGoogleURL(url);
let handleUrl = isGoogleURL(url) || (extensionSettings.allowlist.length!=0 && isAllowlistedURL(url));

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

if (handleUrl && extensionSettings.ignore_youtube && isYouTubeURL(url)) {
handleUrl = false;
Expand All @@ -345,10 +373,6 @@ 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
14 changes: 12 additions & 2 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,23 @@ <h1>Settings</h1>
</label>
</p>

<p>
<label>
<textarea id="allowlist" value="" cols="80">
</textarea>
<br>
Include additional urls in Google Container<br>
<em>(Means use Google Container on these additional urls, e.g. for third party SSO or "Log in with Google". Use one url per line.)</em>
</label>
</p>

<p>
<label>
<textarea id="whitelist" value="" cols="80">
</textarea>
<br>
Whitelisted google urls<br>
<em>(Use one url per line.)</em>
Exclude Google urls in Google Container<br>
<em>(Means don't use Google Container on these Google urls. Use one url per line.)</em>
</label>
</p>

Expand Down
32 changes: 17 additions & 15 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.");
return [];
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.");
continue;
}
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 5fb66dd

Please sign in to comment.