/
config.js
96 lines (83 loc) · 2.8 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Copyright 2017 Craig Miskell
This file is part of CookieMaster, a Firefox Web Extension
CookieMaster is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CookieMaster is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var ThirdPartyOptions = {
AllowAll: 'AllowAll',
AllowNone: 'AllowNone',
AllowIfOtherwiseAllowed: 'AllowIfOtherwiseAllowed',
}
var CookieActions = {
Blocked: 0,
Allowed: 1,
Unset: 2, //Neither blocked nor allowed
}
async function getConfig() {
try {
var config = await browser.storage.local.get();
} catch(e) {
console.error("caught exception");
console.error(e);
}
if(!('ready' in config)) {
resetToFactorySettings();
return browser.storage.local.get();
}
return config;
}
async function resetToFactorySettings() {
var config= {};
config.thirdParty = ThirdPartyOptions.AllowNone
config.allowList = [];
config.ready = true;
await browser.storage.local.set(config);
}
//Returns the configured domain that allowed the named domain, or undefined if none do.
function domainIsAllowed(config, domain) {
//Is a linear search ok? Can't help but wonder if there's a more efficient technique. Maybe a
// transient cache of results would help?
var result = undefined;
//NB: Assumes the 'allow' list has leading dots on all entries, to properly terminate domain
// components
//Domain *must* start with a dot to match the allow list constraint
if(!domain.startsWith('.')) {
domain = '.'+domain;
}
// From https://github.com/craigmiskell/cookiemaster/issues/27: string any trailing dot
// because some sites present that but it's not useful here.
if (domain.endsWith(".")) {
domain = domain.slice(0,-1);
}
for (var candidate of config.allowList) {
if(domain.endsWith(candidate)) {
// Use the longest (most specific) candidate (e.g. www.example.org over example.org) that
// allowed the cookie
if((result == undefined) || (candidate.length > result.length)) {
result = candidate;
}
}
}
return result;
}
//Reduced set of domainIsAllowed, looking for an explicit match (where 'in' doesn't seem to work)
function domainInList(config, domain) {
for(var cd of config.allowList) {
if (domain == cd) {
return true;
}
}
return false;
}
module.exports = {
domainIsAllowed: domainIsAllowed
}