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
|
'use strict';
var sitesUI;
addEventListener("unload", e => {
if (!UI.initialized) {
Messages.send("openStandalonePopup");
}
});
(async () => {
function showMessage(className, message) {
let el = document.getElementById("message");
el.textContent = message;
el.className = className;
}
try {
let tabId;
let pendingReload = false;
let isBrowserAction = true;
let optionsClosed = false;
let tab = (await browser.tabs.query({
windowId: browser.windows ?
(await browser.windows.getLastFocused()).id
: null,
active: true
}))[0];
if (!tab || tab.id === -1) {
log("No tab found to open the UI for");
close();
}
if (tab.url === document.URL) {
isBrowserAction = false;
try {
tabId = parseInt(document.URL.match(/#.*\btab(\d+)/)[1]);
} catch (e) {
close();
}
addEventListener("blur", close);
} else {
tabId = tab.id;
}
await UI.init(tabId);
if (isBrowserAction) {
browser.tabs.onActivated.addListener(e => {
if (e.tabId !== tabId) close();
});
}
await include("/ui/toolbar.js");
{
let clickHandlers = {
"options": e => {
browser.runtime.openOptionsPage();
close();
},
"close": close,
"reload": reload,
"temp-trust-page": e => sitesUI.tempTrustAll(),
"revoke-temp": e => {
UI.revokeTemp();
close();
}
};
for (let [id, handler] of Object.entries(clickHandlers)) {
document.getElementById(id).onclick = handler;
}
}
{
let policy = UI.policy;
let pressed = policy.enforced;
let button = document.getElementById("enforce");
button.setAttribute("aria-pressed", pressed);
button.textContent = button.title = _(pressed ? "NoEnforcement" : "Enforce");
button.onclick = async () => {
this.disabled = true;
policy.enforced = !pressed;
await UI.updateSettings({policy, reloadAffected: true});
close();
}
}
{
let pressed = !UI.unrestrictedTab;
let button = document.getElementById("enforce-tab");
button.setAttribute("aria-pressed", pressed);
button.textContent = button.title = _(pressed ? "NoEnforcementForTab" : "EnforceForTab");
if (UI.policy.enforced) {
button.onclick = async () => {
this.disabled = true;
await UI.updateSettings({
unrestrictedTab: pressed,
reloadAffected: true,
});
close();
}
} else {
button.disabled = true;
}
}
let mainFrame = UI.seen && UI.seen.find(thing => thing.request.type === "main_frame");
debug("Seen: %o", UI.seen);
if (!mainFrame) {
let isHttp = /^https?:/.test(tab.url);
try {
await browser.tabs.executeScript(tabId, { code: "" });
if (isHttp) {
document.body.classList.add("disabled");
showMessage("warning", _("freshInstallReload"));
let buttons = document.querySelector("#buttons");
let b = document.createElement("button");
b.textContent = _("OK");
b.onclick = document.getElementById("reload").onclick = () => {
reload();
close();
}
buttons.appendChild(b);
b = document.createElement("button");
b.textContent = _("Cancel");
b.onclick = () => close();
buttons.appendChild(b);
return;
}
} catch (e) {
error(e, "Could not run scripts on %s: privileged page?", tab.url);
}
await include("/lib/restricted.js");
let isRestricted = isRestrictedURL(tab.url);
if (!isHttp || isRestricted) {
showMessage("warning", _("privilegedPage"));
let tempTrust = document.getElementById("temp-trust-page");
tempTrust.disabled = true;
return;
}
if (!UI.seen) {
if (!isHttp) return;
UI.seen = [
mainFrame = {
request: { url: tab.url, documentUrl: tab.url, type: "main_frame" }
}
];
}
}
let justDomains = !UI.local.showFullAddresses;
sitesUI = new UI.Sites(document.getElementById("sites"));
sitesUI.onChange = (row) => {
pendingReload = !row.temp2perm;
if (optionsClosed) return;
browser.tabs.query({url: browser.runtime.getManifest().options_ui.page })
.then(tabs => {
browser.tabs.remove(tabs.map(t => t.id));
});
optionsClosed = true;
};
initSitesUI();
UI.onSettings = initSitesUI;
function initSitesUI() {
pendingReload = false;
let {
typesMap
} = sitesUI;
typesMap.clear();
let policySites = UI.policy.sites;
let domains = new Map();
function urlToLabel(url) {
let {
origin
} = url;
let match = policySites.match(url);
if (match) return match;
if (domains.has(origin)) {
if (justDomains) return domains.get(origin);
} else {
let domain = tld.getDomain(url.hostname);
domain = url.protocol === "https:" ? Sites.secureDomainKey(domain) : domain;
domains.set(origin, domain);
if (justDomains) return domain;
}
return origin;
}
let seen = UI.seen;
let parsedSeen = seen.map(thing => Object.assign({
type: thing.policyType
}, Sites.parse(thing.request.url)))
.filter(parsed => parsed.url && parsed.url.origin !== "null");
let sitesSet = new Set(
parsedSeen.map(parsed => parsed.label = urlToLabel(parsed.url))
);
if (!justDomains) {
for (let domain of domains.values()) sitesSet.add(domain);
}
let sites = [...sitesSet];
for (let parsed of parsedSeen) {
sites.filter(s => parsed.label === s || domains.get(parsed.url.origin) === s).forEach(m => {
let siteTypes = typesMap.get(m);
if (!siteTypes) typesMap.set(m, siteTypes = new Set());
siteTypes.add(parsed.type);
});
}
sitesUI.mainUrl = new URL(mainFrame.request.url)
sitesUI.mainSite = urlToLabel(sitesUI.mainUrl);
sitesUI.mainDomain = tld.getDomain(sitesUI.mainUrl.hostname);
sitesUI.render(sites);
}
function reload() {
if (sitesUI) sitesUI.clear();
browser.tabs.reload(tabId);
pendingReload = false;
}
function close() {
if (isBrowserAction) {
window.close();
} else {
//browser.windows.remove(tab.windowId);
browser.tabs.remove(tab.id);
}
}
let {
onCompleted
} = browser.webNavigation;
let loadSnapshot = sitesUI.snapshot;
let onCompletedListener = navigated => {
if (navigated.tabId === tabId) {
UI.pullSettings();
}
};
onCompleted.addListener(onCompletedListener, {
url: [{
hostContains: sitesUI.mainDomain
}]
});
addEventListener("unload", e => {
onCompleted.removeListener(onCompletedListener);
debug("pendingReload", pendingReload);
if (pendingReload) {
UI.updateSettings({
policy: UI.policy,
reloadAffected: true,
});
}
}, true);
} catch (e) {
error(e, "Can't open popup");
close();
}
})();
|