/
main-view.js
123 lines (110 loc) · 4.26 KB
/
main-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
/*
Licensed per https://github.com/privacy-tech-lab/gpc-optmeowt/blob/main/LICENSE.md
privacy-tech-lab, https://privacytechlab.org/
*/
/*
main-view.js
================================================================================
main-view.js handles the navigation between different parts of the options page
and loads them when called through the navigation bar
*/
// ! We will probably have to "import" our html docs in order for Webpack to catch them
// ! This means removing the await "docs.html" lines in all of the options page views
import { fetchTemplate, parseTemplate } from "../../components/util.js";
import { settingsView } from "../settings-view/settings-view.js";
import { domainlistView } from "../domainlist-view/domainlist-view.js";
import { analysisView } from "../analysis-view/analysis-view.js";
import { aboutView } from "../about-view/about-view.js";
import { storage, stores } from "../../../background/storage.js";
import Darkmode from "../../../theme/darkmode.js";
/**
* Opens the `Settings` page
* @param {string} bodyTemplate - stringified HTML template
*/
async function displaySettings(bodyTemplate) {
settingsView(bodyTemplate);
document.querySelector(".navbar-item.active").classList.remove("active");
document.querySelector("#main-view-settings").classList.add("active");
}
/**
* Opens the `Domainlist` page
* @param {string} bodyTemplate - stringified HTML template
*/
function displayDomainlist(bodyTemplate) {
domainlistView(bodyTemplate);
document.querySelector(".navbar-item.active").classList.remove("active");
document.querySelector("#main-view-domainlist").classList.add("active");
}
function displayAnalysis(bodyTemplate) {
analysisView(bodyTemplate);
document.querySelector(".navbar-item.active").classList.remove("active");
document.querySelector("#main-view-analysis").classList.add("active");
}
/**
* Opens the `Display` page
* @param {string} bodyTemplate - stringified HTML template
*/
function displayAbout(bodyTemplate) {
aboutView(bodyTemplate);
document.querySelector(".navbar-item.active").classList.remove("active");
document.querySelector("#main-view-about").classList.add("active");
}
/**
* Prepares the `Main` page elements and intializes the default `Settings` page
*/
export async function mainView() {
let docTemplate = await fetchTemplate("./views/main-view/main-view.html");
const bodyTemplate = await fetchTemplate(
"./components/scaffold-component.html"
);
document.body.innerHTML =
parseTemplate(docTemplate).getElementById("main-view").innerHTML;
let domainlistPressed = await storage.get(
stores.settings,
"DOMAINLIST_PRESSED"
);
let analysisPressed = await storage.get(stores.settings, "ANALYSIS_PRESSED");
if (!domainlistPressed && !analysisPressed) {
settingsView(bodyTemplate); // First page
document.querySelector("#main-view-settings").classList.add("active");
} else if (domainlistPressed) {
domainlistView(bodyTemplate); // First page
await storage.set(stores.settings, false, "DOMAINLIST_PRESSED");
document.querySelector("#main-view-domainlist").classList.add("active");
} else if (analysisPressed) {
analysisView(bodyTemplate); // First page
await storage.set(stores.settings, false, "ANALYSIS_PRESSED");
document.querySelector("#main-view-analysis").classList.add("active");
}
document
.getElementById("main-view-settings")
.addEventListener("click", () => displaySettings(bodyTemplate));
document
.getElementById("main-view-domainlist")
.addEventListener("click", () => displayDomainlist(bodyTemplate));
document
.getElementById("main-view-about")
.addEventListener("click", () => displayAbout(bodyTemplate));
if ("$BROWSER" != "firefox") {
document.getElementById("main-view-analysis").style.display = "none";
} else {
document
.getElementById("main-view-analysis")
.addEventListener("click", () => displayAnalysis(bodyTemplate));
}
// DARK MODE
const darkmode = new Darkmode();
//Listener: Listens for a message sent by popup.js
chrome.runtime.onMessage.addListener(function (
message,
sender,
sendResponse
) {
if (message.msg === "DARKSWITCH_PRESSED") {
darkmode.toggle();
}
if (message.msg === "SHOW_TUTORIAL") {
displaySettings(bodyTemplate);
}
});
}