-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
35 lines (31 loc) · 1 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
async function getCurrentTab() {
let queryOptions = { active: true, lastFocusedWindow: true };
// `tab` will either be a `tabs.Tab` instance or `undefined`.
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
const addWebRequestListener = (url) => {
chrome.webRequest.onCompleted.addListener(
function (details) {
console.log(`New request from ${details.initiator}`);
console.log(details);
},
// { urls: ['<all_urls>'] },
{ urls: [`*://${url}/*`] },
['responseHeaders']
);
};
const onChangeTab = async (activeInfo) => {
const { id, url, title, favIconUrl } = await getCurrentTab();
if (!url) return; // Early return if url is undefined (happens on opening a new tab)
const { hostname } = new URL(url);
console.log('Tab changed');
console.log(`Current url: ${url}`);
console.log(`Hostname: ${hostname}`);
if (hostname) {
addWebRequestListener(hostname);
}
};
chrome.tabs.onActivated.addListener((activeInfo) => {
onChangeTab(activeInfo);
});