/
editDomainlist.js
181 lines (155 loc) · 5.71 KB
/
editDomainlist.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Licensed per https://github.com/privacy-tech-lab/gpc-optmeowt/blob/main/LICENSE.md
privacy-tech-lab, https://privacytechlab.org/
*/
/*
editDomainlist.js
================================================================================
editDomainlist.js is an internal API modifying the domainlist / modifying the
domainlist simultaneously with the dynamic ruleset
*/
import { storage, stores } from "../background/storage.js";
import {
deleteAllDynamicRules,
deleteDynamicRule,
addDynamicRule,
getFreshId,
} from "./editRules.js";
// TODO: Migrate editRules & editDomainlist to one file
/*************************** FILE ROADMAP ******************************/
/*
Here are a few functions that are either implemented already or might be
worthwhile to implement down the road (for adding things to the lists
individually, for clearing them on extension changes for reload, etc.)
Go ahead and implement as needed.
*/
/* # Standard Operation -- functions during domainlist lifetime */
// addDomainToDLDR
// removeDomainFromDLDR
// deleteDomainFromDLDR
/* # Big Operations -- generally for changing modes */
// deleteAllDynamicRules
// deleteAllFromDomainlist
// syncDynamicRulesFromDomainlist // for domainlist uploads
// syncDomainlistFromDynamicRules
/* ~~Maybe~~ */
// getDomainFromId or queryDomain
// getIdFromDomain or queryId
// removeRule(id)
// removeDOmain(domain) // implicitly remove rule if it exists
// addDomain(domain) // No ID
// removeDomainRule(domain) // only remove domain rule
// removerule(id) // (don't really need id...)
// addRule(domain, id) // implicitly fresh id
/* # Debugging */
// debug_domainlist_and_dynamicrules
// print_rules_and_domainlist
/******************************************************************************/
/******************************************************************************/
/********** # Standard Operation **********/
/******************************************************************************/
/******************************************************************************/
async function updateRemovalScript() {
if ("$BROWSER" == "chrome") {
let matches = ["https://example.org/foo/bar.html"];
let domain;
let domainValue;
const domainlistKeys = await storage.getAllKeys(stores.domainlist);
const domainlistValues = await storage.getAll(stores.domainlist);
for (let index in domainlistKeys) {
domain = domainlistKeys[index];
domainValue = domainlistValues[index];
if (domainValue != null) {
matches.push("https://" + domain + "/*");
matches.push("https://www." + domain + "/*");
}
}
chrome.scripting
.updateContentScripts([
{
id: "2",
matches: matches,
js: ["content-scripts/registration/gpc-remove.js"],
runAt: "document_start",
},
])
.then(() => {});
}
}
async function deleteDomainlistAndDynamicRules() {
await storage.clear(stores.domainlist);
if ("$BROWSER" == "chrome") {
deleteAllDynamicRules();
}
}
async function addDomainToDomainlistAndRules(domain) {
let id = 1;
if ("$BROWSER" == "chrome") {
id = await getFreshId();
addDynamicRule(id, domain); // add the rule for the chosen domain
}
await storage.set(stores.domainlist, id, domain); // record what rule the domain is associated to
}
async function removeDomainFromDomainlistAndRules(domain) {
if ("$BROWSER" == "chrome") {
let id = await storage.get(stores.domainlist, domain);
deleteDynamicRule(id);
}
await storage.set(stores.domainlist, null, domain);
}
/******************************************************************************/
/******************************************************************************/
/********** # Debugging **********/
/******************************************************************************/
/******************************************************************************/
async function debug_domainlist_and_dynamicrules() {
let sampleSites = [
"a.com",
"b.com",
"c.com",
"d.com",
"e.com",
"f.com",
"g.com",
"h.com",
"i.com",
"j.com",
];
await deleteDomainlistAndDynamicRules();
await print_rules_and_domainlist();
addDynamicRule(2, "nytimes.com"); // add the rule for the chosen domain
await storage.set(stores.domainlist, 2, "nytimes.com"); // record what rule the domain is associated to
await print_rules_and_domainlist();
await addDomainToDomainlistAndRules("a.com");
await addDomainToDomainlistAndRules("b.com");
await addDomainToDomainlistAndRules("c.com");
await addDomainToDomainlistAndRules("d.com");
await addDomainToDomainlistAndRules("e.com");
await addDomainToDomainlistAndRules("f.com");
await addDomainToDomainlistAndRules("g.com");
await addDomainToDomainlistAndRules("h.com");
await addDomainToDomainlistAndRules("i.com");
await addDomainToDomainlistAndRules("j.com");
await print_rules_and_domainlist();
}
async function print_rules_and_domainlist() {
let rules = await chrome.declarativeNetRequest.getDynamicRules();
let domainlist = await storage.getStore(stores.domainlist);
console.log(
"Here are the curr dynamic rules:",
rules,
"Here is our curr domainlist: ",
domainlist
);
}
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
export {
deleteDomainlistAndDynamicRules,
addDomainToDomainlistAndRules,
removeDomainFromDomainlistAndRules,
updateRemovalScript,
debug_domainlist_and_dynamicrules,
print_rules_and_domainlist,
};