forked from smebberson/varnish-indicator-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
59 lines (52 loc) · 1.45 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
const button = {
active: false,
status: null,
};
const buttonActive = {
[true]: 'blue',
[false]: 'grey',
};
const buttonStatus = {
hit: 'green',
miss: 'red',
};
const isVarnishPresent = ({ headers = [] } = {}) =>
headers.some(({ name }) => ['x-varnish', 'x-cache'].includes(name));
const varnishStatus = ({ headers = [] } = {}) => {
const varnishHeaders = headers.filter(({ name }) => name === 'x-cache');
console.log('varnishHeadesr');
console.log(varnishHeaders);
if (varnishHeaders.length) {
return varnishHeaders.some((header) => {
console.log(header.value);
return header.value.includes('hit');
})
? 'hit'
: 'miss';
}
return null;
};
chrome.webRequest.onHeadersReceived.addListener(
(details) => {
if (details.type === 'main_frame') {
const headers = details.responseHeaders;
button.active = isVarnishPresent({ headers });
button.status = varnishStatus({ headers });
}
},
{
urls: ['http://*/*', 'https://*/*'],
},
['responseHeaders'],
);
chrome.webNavigation.onCompleted.addListener((details) => {
if (details.frameId === 0) {
chrome.pageAction.setIcon({
path: `img/${
(button.status && buttonStatus[button.status]) ||
buttonActive[button.active]
}.png`,
tabId: details.tabId,
});
}
});