diff --git a/background.js b/background.js index ba755d0..61d4b99 100644 --- a/background.js +++ b/background.js @@ -54,6 +54,7 @@ const tabsWaitingToLoad = {}; const googleHostREs = []; const youtubeHostREs = []; const whitelistedHostREs = []; +const allowlistedHostREs = []; async function isMACAddonEnabled () { try { @@ -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 () { @@ -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'); @@ -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; @@ -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) { diff --git a/options.html b/options.html index a5f3ddd..877f854 100644 --- a/options.html +++ b/options.html @@ -62,13 +62,23 @@
+ +
+diff --git a/options.js b/options.js index 2727c12..4eafd37 100644 --- a/options.js +++ b/options.js @@ -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 : ""; } @@ -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(); @@ -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"); }); }