-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
42 lines (35 loc) · 1.25 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
let contentScriptReady = false;
const manipators = [
"https://www.youtube.com"
]
chrome.runtime.onMessage.addListener((request) => {
console.log(request, "request");
if (request.action === "contentScriptReady") {
contentScriptReady = true;
}
});
chrome.webRequest.onBeforeSendHeaders.addListener(
(details) => {
if (details.type === "xmlhttprequest" &&!details.initiator.includes(manipators)) {
const method = details.method.toUpperCase();
const url = details.url;
const headers = {};
const lastIndex = url.lastIndexOf('/');
const displayedKey = lastIndex > 0
? `${url.slice(0, 20)}...${url.slice(lastIndex)}`
: url.slice(0, 20) + "...";
for (const header of details.requestHeaders) {
headers[header.name] = header.value;
}
const fetchCode = `fetch("${url}", {
method: "${method}",
${Object.keys(headers).length > 0? `headers: { ${Object.entries(headers).map(([key, value]) => `${key}: ${value.replace(/"/g, '\\"')}`).join(", ")} },` : ""}
});`;
if (contentScriptReady) {
chrome.runtime.sendMessage({ action: "addFetchRequest", fetchCode, requestName: `${method} ${displayedKey}` });
}
}
},
{ urls: ["<all_urls>"] },
["requestHeaders"]
);