/
settings-view.js
315 lines (287 loc) · 8.92 KB
/
settings-view.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
Licensed per https://github.com/privacy-tech-lab/gpc-optmeowt/blob/main/LICENSE.md
privacy-tech-lab, https://privacytechlab.org/
*/
/*
settings-view.js
================================================================================
settings-view.js loads settings-view.html when clicked on the options page
*/
import { renderParse, fetchParse } from "../../components/util.js";
import {
handleDownload,
startUpload,
handleUpload,
stores,
storage,
// extensionMode
} from "../../../background/storage.js";
import { csvGenerator } from "../../../common/csvGenerator.js";
import { modes } from "../../../data/modes.js";
// Used in tutorial
import UIkit from "../../../../node_modules/uikit/dist/js/uikit.js";
import tippy from "../../../../node_modules/tippy.js/dist/tippy-bundle.umd.js";
import "../../../../node_modules/file-saver/src/FileSaver.js";
import Darkmode from "darkmode-js";
import {
addDynamicRule,
deleteAllDynamicRules,
reloadDynamicRules,
} from "../../../common/editRules.js";
import { updateRemovalScript } from "../../../common/editDomainlist.js";
// Global scope settings variable
var mode;
/**
* @typedef headings
* @property {string} headings.title - Title of the given page
* @property {string} headings.subtitle - Subtitle of the given page
*/
const headings = {
title: "Settings",
subtitle: "Adjust extension settings",
};
function handleDownloadAnalysis() {
chrome.runtime.sendMessage({
msg: "CSV_DATA_REQUEST_FROM_SETTINGS",
});
}
/**
* Creates the event listeners for the `Settings` page buttons and options
*/
function eventListeners() {
document
.getElementById("settings-view-radio0")
.addEventListener("click", () => {
chrome.runtime.sendMessage({
msg: "TURN_ON_OFF",
data: { isEnabled: true },
});
chrome.runtime.sendMessage({
msg: "CHANGE_IS_DOMAINLISTED",
data: { isDomainlisted: false },
});
chrome.runtime.sendMessage({
msg: "CHANGE_MODE",
data: modes.protection,
});
if ("$BROWSER" == "chrome") {
chrome.scripting.updateContentScripts([
{
id: "2",
matches: ["https://example.org/foo/bar.html"],
js: ["content-scripts/registration/gpc-remove.js"],
runAt: "document_start",
},
]);
deleteAllDynamicRules();
}
});
document
.getElementById("settings-view-radio1")
.addEventListener("click", () => {
chrome.runtime.sendMessage({
msg: "TURN_ON_OFF",
data: { isEnabled: false },
});
chrome.runtime.sendMessage({
msg: "CHANGE_IS_DOMAINLISTED",
data: { isDomainlisted: false },
});
if ("$BROWSER" == "chrome") {
chrome.scripting.updateContentScripts([
{
id: "2",
matches: ["<all_urls>"],
js: ["content-scripts/registration/gpc-remove.js"],
runAt: "document_start",
},
]);
addDynamicRule(4999, "*");
}
});
document
.getElementById("settings-view-radio2")
.addEventListener("click", () => {
chrome.runtime.sendMessage({
msg: "TURN_ON_OFF",
data: { isEnabled: true },
});
chrome.runtime.sendMessage({
msg: "CHANGE_IS_DOMAINLISTED",
data: { isDomainlisted: true },
});
chrome.runtime.sendMessage({
msg: "CHANGE_MODE",
data: modes.protection,
});
if ("$BROWSER" == "chrome") {
updateRemovalScript();
reloadDynamicRules();
}
});
document
.getElementById("download-button")
.addEventListener("click", handleDownload);
document
.getElementById("download-analysis-button")
.addEventListener("click", handleDownloadAnalysis);
document.getElementById("upload-button").addEventListener("click", () => {
const verify = confirm(
`This option will load a list of domains from a file, clearing all domains currently in the list.\n Do you wish to continue?`
);
if (verify) {
startUpload();
}
});
document
.getElementById("upload-domainlist")
.addEventListener("change", handleUpload, false);
chrome.runtime.onMessage.addListener(async function (message, _, __) {
if (message.msg === "SHOW_TUTORIAL") {
if ("$BROWSER" == "chrome") {
chrome.tabs.reload();
} else {
await storage.set(stores.settings, true, "TUTORIAL_SHOWN");
walkthrough();
}
}
});
createMessageListeners();
}
function createMessageListeners() {
chrome.runtime.onMessage.addListener(async function (message, _, __) {
const csvData = await storage.getStore(stores.analysis);
if (message.msg === "CSV_DATA_RESPONSE_TO_SETTINGS") {
csvGenerator(csvData, message.data.titles);
return;
}
});
}
/******************************************************************************/
/*
* Gives user a walkthrough of install page on first install
*/
function analysisWarning() {
let modal = UIkit.modal("#analysis-modal");
modal.show();
document.getElementById("modal-button-5").onclick = function () {
modal.hide();
};
}
function walkthrough() {
let modal = UIkit.modal("#welcome-modal");
modal.show();
document.getElementById("modal-button-1").onclick = function () {
modal.hide();
};
document.getElementById("modal-button-2").onclick = function () {
modal.hide();
tippy(".tutorial-tooltip1", {
content:
"<p>Set which sites should receive a Do Not Sell signal<p> <button class='uk-button uk-button-default'>Next</button>",
allowHTML: true,
trigger: "manual",
placement: "right",
offset: [0, -600],
duration: 1000,
theme: "custom-1",
onHide(instance) {
trigger2();
},
});
let tooltip =
document.getElementsByClassName("tutorial-tooltip1")[0]._tippy;
tooltip.show();
};
function trigger2() {
tippy(".tutorial-tooltip2", {
content:
"<p>Import and export your customized list of sites that should receive a signal<p> <button class='uk-button uk-button-default'>Next</button>",
allowHTML: true,
trigger: "manual",
duration: 1000,
theme: "custom-1",
placement: "right",
offset: [0, 60],
onHide() {
trigger4();
},
});
let tooltip =
document.getElementsByClassName("tutorial-tooltip2")[0]._tippy;
tooltip.show();
}
function trigger4() {
let modal = UIkit.modal("#thank-you-modal");
modal.show();
document.getElementById("modal-button-3").onclick = () => {
chrome.tabs.create(
{ url: "https://privacytechlab.org/optmeowt" },
function (tab) {}
);
};
}
}
// Show Analysis Warning
async function initPopUpWalkthrough() {
const analysisWarningShown = await storage.get(
stores.settings,
"ANALYSIS_WARNING_SHOWN"
);
if (!analysisWarningShown && mode === modes.analysis) {
analysisWarning();
storage.set(stores.settings, true, "ANALYSIS_WARNING_SHOWN");
}
}
// Button to change to Analysis Mode
function loadChangeMode() {
if ("$BROWSER" != "firefox") {
document.getElementById("optMode-parent").style.display = "none";
document.getElementById("analysis-export").style.display = "none";
} else {
document.getElementById("optMode").addEventListener("click", function () {
mode = modes.analysis;
chrome.runtime.sendMessage({ msg: "CHANGE_MODE", data: mode });
chrome.runtime.sendMessage({
msg: "TURN_ON_OFF",
data: { isEnabled: true },
});
initPopUpWalkthrough();
});
}
}
/******************************************************************************/
/**
* Renders the `Settings` view in the options page
* @param {string} scaffoldTemplate - stringified HTML template
*/
export async function settingsView(scaffoldTemplate) {
const body = renderParse(scaffoldTemplate, headings, "scaffold-component");
let content = await fetchParse(
"./views/settings-view/settings-view.html",
"settings-view"
);
document.getElementById("content").innerHTML = body.innerHTML;
document.getElementById("scaffold-component-body").innerHTML =
content.innerHTML;
// Render correct extension mode radio button
const isEnabled = await storage.get(stores.settings, "IS_ENABLED");
const isDomainlisted = await storage.get(stores.settings, "IS_DOMAINLISTED");
let mode = await storage.get(stores.settings, "MODE");
if (mode === modes.analysis) {
document.getElementById("optMode").checked = true;
} else if (isEnabled) {
isDomainlisted
? (document.getElementById("settings-view-radio2").checked = true)
: (document.getElementById("settings-view-radio0").checked = true);
} else {
document.getElementById("settings-view-radio1").checked = true;
}
eventListeners();
loadChangeMode();
const tutorialShown = await storage.get(stores.settings, "TUTORIAL_SHOWN");
if (!tutorialShown) {
walkthrough();
}
storage.set(stores.settings, true, "TUTORIAL_SHOWN");
}