/
control.js
175 lines (157 loc) · 4.75 KB
/
control.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
/*
Licensed per https://github.com/privacy-tech-lab/gpc-optmeowt/blob/main/LICENSE.md
privacy-tech-lab, https://privacytechlab.org/
*/
/*
control.js
================================================================================
control.js manages persistent data, message liseteners, in particular
to manage the state & functionality mode of the extension
*/
import {
init as initProtection_ff,
halt as haltProtection_ff,
} from "./protection/protection-ff.js";
import {
init as initProtection_cr,
halt as haltProtection_cr,
} from "./protection/protection.js";
import {
init as initAnalysis,
halt as haltAnalysis,
} from "./analysis/analysis.js";
import { defaultSettings } from "../data/defaultSettings.js";
import { modes } from "../data/modes.js";
import { stores, storage } from "./storage.js";
import { reloadDynamicRules } from "../common/editRules.js";
// TODO: Remove
import {
debug_domainlist_and_dynamicrules,
updateRemovalScript,
} from "../common/editDomainlist.js";
async function enable() {
let mode = await storage.get(stores.settings, "MODE");
if ("$BROWSER" == "firefox") {
var initProtection = initProtection_ff;
var haltProtection = haltProtection_ff;
} else {
var initProtection = initProtection_cr;
var haltProtection = haltProtection_cr;
}
switch (mode) {
case modes.analysis:
initAnalysis();
haltProtection();
break;
case modes.protection:
initProtection();
haltAnalysis();
break;
default:
initProtection();
haltAnalysis();
await storage.set(stores.settings, modes.protection, "MODE");
break;
}
}
function disable() {
if ("$BROWSER" == "firefox") {
var haltProtection = haltProtection_ff;
} else if ("$BROWSER" == "chrome") {
var haltProtection = haltProtection_cr;
}
haltAnalysis();
haltProtection();
}
/******************************************************************************/
// Initializers
// This is the very first thing the extension runs
(async () => {
// TODO: Temporarily register content script
if ("$BROWSER" == "chrome") {
chrome.scripting.registerContentScripts([
{
id: "1",
matches: ["<all_urls>"],
js: ["content-scripts/registration/gpc-dom.js"],
runAt: "document_start",
},
{
id: "2",
matches: ["https://example.org/foo/bar.html"],
js: ["content-scripts/registration/gpc-remove.js"],
runAt: "document_start",
},
]);
}
// Initializes the default settings
let settingsDB = await storage.getStore(stores.settings);
for (let setting in defaultSettings) {
if (!settingsDB[setting]) {
await storage.set(stores.settings, defaultSettings[setting], setting);
}
}
let isEnabled = await storage.get(stores.settings, "IS_ENABLED");
if (isEnabled) {
// Turns on the extension
enable();
}
if ("$BROWSER" == "chrome") {
updateRemovalScript();
reloadDynamicRules();
}
})();
/******************************************************************************/
// Mode listeners
// (1) Handle extension activeness is changed by calling all halt
// - Make sure that I switch extensionmode and separate it from mode.domainlist
// (2) Handle extension functionality with listeners and message passing
/**
* Listeners for information from --POPUP-- or --OPTIONS-- page
* This is the main "hub" for message passing between the extension components
* https://developer.chrome.com/docs/extensions/mv3/messaging/
*/
chrome.runtime.onMessage.addListener(async function (
message,
sender,
sendResponse
) {
if (message.msg === "TURN_ON_OFF") {
let isEnabled = message.data.isEnabled; // can be undefined
if (isEnabled) {
await storage.set(stores.settings, true, "IS_ENABLED");
enable();
} else {
await storage.set(stores.settings, false, "IS_ENABLED");
disable();
}
}
if (message.msg === "CHANGE_MODE") {
let mode = message.data;
let isEnabled = await storage.get(stores.settings, "IS_ENABLED");
await storage.set(stores.settings, mode, "MODE");
if (isEnabled) {
enable();
}
chrome.runtime.sendMessage({
msg: "RELOAD_DUE_TO_MODE_CHANGE",
data: mode,
});
}
if (message.msg === "CHANGE_IS_DOMAINLISTED") {
let isDomainlisted = message.data.isDomainlisted; // can be undefined
}
});
// Handles requests for global mode
/**
* IF YOU EVER NEED TO DEBUG THIS:
* This is outmoded in manifest V3. We cannot maintain global variables anymore.
*/
chrome.runtime.onConnect.addListener(function (port) {
port.onMessage.addListener(async function (message) {
let mode = await storage.get(stores.settings, "MODE");
if (message.msg === "REQUEST_MODE") {
port.postMessage({ msg: "RESPONSE_MODE", data: mode });
}
});
});