Skip to content

Commit 225329c

Browse files
committed
Chore: add allowlist on preferences
1 parent 48c32da commit 225329c

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

background.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const tabsWaitingToLoad = {};
5454
const googleHostREs = [];
5555
const youtubeHostREs = [];
5656
const whitelistedHostREs = [];
57+
const allowlistedHostREs = [];
5758

5859
async function isMACAddonEnabled () {
5960
try {
@@ -171,11 +172,23 @@ function generateWhitelistedHostREs () {
171172
}
172173
}
173174

175+
function generateAllowlistedHostREs () {
176+
if (allowlistedHostREs.length != 0) {return;}
177+
const matchOperatorsRegex = /[|\\{}()[\]^$+*?.-]/g;
178+
for (let allowlistedDomain of extensionSettings.allowlist) {
179+
allowlistedDomain = allowlistedDomain.replace(matchOperatorsRegex, '\\$&');
180+
allowlistedHostREs.push(new RegExp(`(^|\\.)${allowlistedDomain}$`));
181+
}
182+
}
183+
174184
async function loadExtensionSettings () {
175185
extensionSettings = await browser.storage.sync.get();
176186
if (extensionSettings.whitelist === undefined){
177187
extensionSettings.whitelist = "";
178188
}
189+
if (extensionSettings.allowlist === undefined){
190+
extensionSettings.allowlist = "";
191+
}
179192
}
180193

181194
async function clearGoogleCookies () {
@@ -288,6 +301,17 @@ function isWhitelistedURL (url) {
288301
return false;
289302
}
290303

304+
function isAllowlistedURL (url) {
305+
generateAllowlistedHostREs();
306+
const parsedUrl = new URL(url);
307+
for (let allowlistedHostRE of allowlistedHostREs) {
308+
if (allowlistedHostRE.test(parsedUrl.host)) {
309+
return true;
310+
}
311+
}
312+
return false;
313+
}
314+
291315
function isSearchPageURL (url) {
292316
const parsedUrl = new URL(url);
293317
return parsedUrl.pathname.startsWith('/search');
@@ -349,6 +373,10 @@ function shouldContainInto (url, tab) {
349373
handleUrl = false;
350374
}
351375

376+
if (!handleUrl && extensionSettings.allowlist.length!=0 && isAllowlistedURL(url)) {
377+
handleUrl = true;
378+
}
379+
352380
if (handleUrl) {
353381
if (tab.cookieStoreId !== googleCookieStoreId) {
354382
if (tab.cookieStoreId !== "firefox-default" && extensionSettings.dont_override_containers) {

options.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ <h1>Settings</h1>
7272
</label>
7373
</p>
7474

75+
<p>
76+
<label>
77+
<textarea id="allowlist" value="" cols="80">
78+
</textarea>
79+
<br>
80+
Allowlisted urls to use Google Container<br>
81+
<em>(Use one url per line.)</em>
82+
</label>
83+
</p>
84+
7585
<button type="submit">Save settings</button>
7686

7787
<hr>

options.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
function validate_whitelist() {
1+
function validate_list(htmlId) {
22
domain_regex = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/;
3-
whitelist_element = document.querySelector("#whitelist");
4-
if (whitelist_element.value == "") {return [];}
5-
whitelist_domains = whitelist_element.value.split("\n");
3+
list_element = document.querySelector(htmlId);
4+
if (list_element.value == "") {return [];}
5+
list_domains = list_element.value.split("\n");
66
validated_domains = [];
7-
for (whitelist_domain of whitelist_domains) {
8-
if (whitelist_domain == "") {continue;}
9-
if (whitelist_domain.match(domain_regex)) {validated_domains.push(whitelist_domain); continue;}
10-
alert("'" + whitelist_domain + "' is not a valid domain.");
7+
for (list_domain of list_domains) {
8+
if (list_domain == "") {continue;}
9+
if (list_domain.match(domain_regex)) {validated_domains.push(list_domain); continue;}
10+
alert("'" + list_domain + "' is not a valid domain.");
1111
return [];
1212
}
1313
return validated_domains;
1414
}
1515

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

2121

@@ -30,8 +30,9 @@ function onOptionsPageSave(e)
3030
"ignore_prefpages": document.querySelector("#ignore_prefpages").checked,
3131
"ignore_maps": document.querySelector("#ignore_maps").checked,
3232
"ignore_flights": document.querySelector("#ignore_flights").checked,
33-
"dont_override_containers": document.querySelector("#dont_override_containers").checked,
34-
"whitelist": validate_whitelist()
33+
"dont_override_containers": document.querySelector("#dont_override_containers").checked,
34+
"whitelist": validate_list("#whitelist"),
35+
"allowlist": validate_list("#allowlist")
3536
});
3637

3738
browser.runtime.reload();
@@ -49,7 +50,8 @@ function onOptionsPageLoaded()
4950
document.querySelector("#ignore_maps").checked = res.ignore_maps || false;
5051
document.querySelector("#ignore_flights").checked = res.ignore_flights || false;
5152
document.querySelector("#dont_override_containers").checked = res.dont_override_containers || false;
52-
fill_whitelist_option(res.whitelist);
53+
fill_list_option(res.whitelist, "#whitelist");
54+
fill_list_option(res.allowlist, "#allowlist");
5355
});
5456
}
5557

0 commit comments

Comments
 (0)