-
Notifications
You must be signed in to change notification settings - Fork 10
/
background.js
94 lines (87 loc) · 2.09 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
91
92
93
94
const requests = chrome.webRequest.onHeadersReceived;
const storage = chrome.storage.local;
let status = true;
listener();
chrome.runtime.onMessage.addListener(fromPopup);
async function listener() {
requests.addListener(
record,
{
urls: ["<all_urls>"],
},
["responseHeaders"]
);
if (await chrome.offscreen.hasDocument()) {
console.debug("Offscreen doc already exists.");
} else {
console.debug("Creating a new offscreen document.");
await chrome.offscreen.createDocument({
url: "./offscreen/offscreen.html",
reasons: [chrome.offscreen.Reason.DOM_PARSER],
justification: "Parse xml",
});
}
}
function fromPopup(toRecord, sender, sendResponse) {
if (toRecord) {
if (requests.hasListener(record)) {
requests.removeListener(record);
status = false;
chrome.action.setBadgeText({
text: "!",
});
chrome.action.setBadgeBackgroundColor({
color: "orange",
});
} else {
listener();
status = true;
chrome.action.setBadgeText({
text: "",
});
}
} else {
sendResponse({
record: status,
});
}
}
async function getBuckets() {
const stored = await storage.get();
const buckets = Object.keys(stored);
return buckets;
}
async function record(response) {
const headers = response.responseHeaders;
const s3 = headers.filter((header) => header.name == "x-amz-request-id")[0];
if (headers.indexOf(s3) != -1) {
let { hostname, pathname } = new URL(response.url);
const buckets = await getBuckets();
if (hostname == "s3.amazonaws.com") {
hostname += "/" + pathname.split("/")[1];
}
let noFavicon = !hostname.includes("favicon.ico");
if (!buckets.includes(hostname) && noFavicon) {
storage.set({
[hostname]: {},
});
await info(hostname);
addNumber();
}
}
}
async function info(hostname) {
let response = await chrome.runtime.sendMessage({ hostname: hostname });
storage.set({
[response.hostname]: response.permissions,
});
}
async function addNumber() {
const buckets = await getBuckets();
chrome.action.setBadgeText({
text: buckets.length.toString(),
});
chrome.action.setBadgeBackgroundColor({
color: "green",
});
}