-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
90 lines (77 loc) · 3.04 KB
/
background.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
(function () {
// ensure other code doesn't blow up
async function initializeStore() {
const store = await browser.storage.local.get();
if (!store) {
store = {};
}
if (!store.data) {
store.data = {};
}
if (!store.options) {
store.options = {};
}
await browser.storage.local.set(store);
}
function getOriginUrl(requestDetails) {
if (isChrome) {
return normalizeUrl(requestDetails.initiator ?? requestDetails.url);
}
return normalizeUrl(requestDetails.originUrl ?? requestDetails.url);
}
// set notification text if site has warnings
async function setNotificationText(url) {
const store = await browser.storage.local.get();
const data = store.data[url] ?? {};
const warnings = Object.keys(data).length;
const text = warnings === 0 ? '' : `${warnings}`;
browser.browserAction.setBadgeText({
text
});
}
async function handleHeaders(requestDetails) {
await Handlers.handleCors(requestDetails, setNotificationText);
await Handlers.handleClickJacking(requestDetails, setNotificationText);
await Handlers.handleReferrer(requestDetails, setNotificationText);
await Handlers.handleHSTS(requestDetails, setNotificationText);
await Handlers.handleMimeSniffing(requestDetails, setNotificationText);
await Handlers.handleServer(requestDetails, setNotificationText);
await Handlers.handleContentSecurityPolicy(requestDetails, setNotificationText);
}
async function onHeadersReceived(requestDetails) {
const originUrl = getOriginUrl(requestDetails);
const store = await browser.storage.local.get();
// if site has no existing data, add an empty data object
if (store.data[originUrl] == null) {
store.data[originUrl] = {};
}
await browser.storage.local.set(store);
await handleHeaders(requestDetails);
try {
const tabs = await browser.tabs.query({ currentWindow: true, active: true })
if (tabs && tabs.length && normalizeUrl(tabs[0].url) == originUrl) {
await setNotificationText(normalizeUrl(tabs[0].url));
}
} catch (err) {
console.log('Error querying tabs', err);
}
}
initializeStore();
// set notification when switching to a new tab
browser.tabs.onActivated.addListener(async (activeInfo) => {
setTimeout(async () => {
try {
const tabs = await browser.tabs.query({ currentWindow: true, active: true });
if (tabs.length) {
await setNotificationText(normalizeUrl(tabs[0].url));
}
} catch (err) {
console.log('Error querying tabs', err);
}
}, 100);
});
browser.webRequest.onHeadersReceived.addListener(
onHeadersReceived,
{ urls: ["<all_urls>"] },
["responseHeaders"]);
})();