forked from edmundkwok/varnish-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
89 lines (87 loc) · 2.53 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
var buttons = [];
chrome.webRequest.onHeadersReceived.addListener(function (details) {
if(!(details.tabId in buttons)) {
buttons[details.tabId] = {
'active': false,
'status': null,
'hits': null
};
}
if (details.type === 'main_frame') {
var headers = details.responseHeaders;
for (var i = 0; i < headers.length; i++) {
var header = headers[i];
if (header.name === 'Via' && header.value.indexOf('varnish')) {
// Only set the popup if page is cached via Varnish. Somehow the tabId will
// only available after some delay.
setTimeout(function(){
chrome.browserAction.setPopup({
tabId: details.tabId,
popup: 'status.html'
});
}, 1000);
buttons[details.tabId].active = true;
}
if (header.name === 'X-Varnish-Cache') {
if (header.value.indexOf('HIT') !== -1) {
buttons[details.tabId].status = 'hit';
} else if (header.value.indexOf('MISS') !== -1) {
buttons[details.tabId].status = 'miss';
}
}
if(header.name === 'X-Varnish-Hits') {
buttons[details.tabId].hits = header.value;
}
}
}
}, {
urls: [
"http://*/*",
"https://*/*"
]
}, [ 'responseHeaders' ]);
chrome.webNavigation.onCompleted.addListener(function(details) {
if (details.frameId === 0) {
var color = (buttons[details.tabId].active) ? 'blue' : 'gray';
if(buttons[details.tabId].status !== null) {
switch (buttons[details.tabId].status) {
case 'hit':
color = 'green';
chrome.browserAction.setBadgeBackgroundColor({
color: [0, 160, 0, 200],
tabId: details.tabId
});
chrome.browserAction.setBadgeText({
text: buttons[details.tabId].hits,
tabId: details.tabId
});
break;
case 'miss':
color = 'red';
chrome.browserAction.setBadgeBackgroundColor({
color: [255, 0, 0, 255],
tabId: details.tabId
});
chrome.browserAction.setBadgeText({
text: 'MISS',
tabId: details.tabId
});
break;
}
}
else {
chrome.browserAction.setBadgeText({
text: '',
tabId: details.tabId
});
chrome.browserAction.setBadgeBackgroundColor({
color: [0, 0, 0, 0],
tabId: details.tabId
});
}
chrome.browserAction.setIcon({
path: 'img/button_' + color + '.png',
tabId: details.tabId
});
}
});