generated from hchiam/learning-firefox-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
151 lines (133 loc) · 4.07 KB
/
popup.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
d = document;
d.$ = document.querySelector;
d.$$ = document.querySelectorAll;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const tab = tabs[0];
const url = new URL(tab.url);
const domain = url.hostname.replace(/^www\./, ""); // example: wikipedia.org
const checkDomainButton = d.$("#check-1");
if (domain === "") {
checkDomainButton.remove();
return;
}
checkDomainButton.textContent = "Check just " + domain;
checkDomainButton.addEventListener("click", () => {
openInNewTab(domain);
window.close();
});
});
d.$("#check").addEventListener("click", async () => {
getHosts((hosts) => {
if (!hosts) {
suggestManualForFirst();
} else {
showCheckboxes(hosts);
}
});
});
d.$("#version-number").firstChild.nodeValue =
chrome.runtime.getManifest().version;
function getHosts(callback) {
chrome.storage.local.get(["hosts"], (results) => {
const hosts = results.hosts;
callback(hosts);
});
}
function suggestManualForFirst() {
alert(
"Something went wrong.\n\nTry copying the current page's URL and running a scan here: https://www.urlvoid.com"
);
openInNewTab();
}
function openAllHosts(hosts) {
for (const host of hosts) {
openInNewTab(host);
}
}
function openInNewTab(host) {
const urlToOpen = "https://www.urlvoid.com/scan/" + (host ? host : "");
chrome.tabs.create({
url: urlToOpen,
});
}
function hideCheckboxes() {
d.$("#checkbox-container").innerText = "";
}
function showCheckboxes(hosts) {
const options = hosts.map(
(host, i) =>
`<label for="host_${i}">
<input type="checkbox" id="host_${i}" class="host" aria-label="${host}" value="${host}" />
${host}
</label>`
);
d.$("#checkbox-container").innerHTML = `
<h2>Choose which ones to check:</h2>
${options.join("")}
<button id="check-selected-hosts" disabled>Check these ^</button>`;
d.$("#check").setAttribute("disabled", true);
d.$$("#checkbox-container input").forEach((input) =>
input.addEventListener("change", enableCheckSelectedHostsButton)
);
d.$("#check-selected-hosts").addEventListener("click", () => {
checkSelectedHosts();
window.close();
});
}
function enableCheckSelectedHostsButton() {
const checkedCount = d.$$("#checkbox-container input:checked").length;
if (checkedCount === 0) {
d.$("#check-selected-hosts").setAttribute("disabled", true);
d.$("#check-selected-hosts").classList.remove("changing-colour");
} else {
d.$("#check-selected-hosts").removeAttribute("disabled");
d.$("#check-selected-hosts").classList.add("changing-colour");
}
d.$("#check-selected-hosts").innerText =
checkedCount === 1 ? "Check this (1) ^" : `Check these (${checkedCount}) ^`;
}
function checkSelectedHosts() {
const hosts = getSelectedHosts();
openAllHosts(hosts);
}
function getSelectedHosts() {
const checkedHostElements = d.$$("#checkbox-container .host:checked");
const values = [];
checkedHostElements.forEach((c) => values.push(c.value));
return values;
}
async function customConfirm(message) {
const container = d.$("#custom-confirm");
const messageElement = container.querySelector(".message");
const cancelButton = container.querySelector(".cancel");
const okButton = container.querySelector(".confirm");
showCustomConfirm();
messageElement.innerText = message;
let awaitingUserAction = true;
let userResponse = false;
function callback(ok) {
awaitingUserAction = false;
userResponse = ok;
}
cancelButton.addEventListener("click", () => {
hideCustomConfirm();
callback(false);
});
okButton.addEventListener("click", () => {
hideCustomConfirm();
callback(true);
});
while (awaitingUserAction) await delay(300);
return userResponse;
}
function showCustomConfirm() {
const container = d.$("#custom-confirm");
container.style.height = "100%";
container.style.padding = "1rem";
}
function hideCustomConfirm() {
const container = d.$("#custom-confirm");
container.style.height = 0;
container.style.padding = 0;
}
const delay = (ms) => new Promise((res) => setTimeout(res, ms));