-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
97 lines (81 loc) · 2.41 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
95
96
97
var score = "";
var xhr = new XMLHttpRequest();
var hasClicked = false;
var currentURL = "";
var currentTab = "";
// Fire when user clicks icon
chrome.browserAction.onClicked.addListener(function (tab) {
var host = tab.url.match(/^[\w-]+:\/*\[?([\w\.:-]+)\]?(?::\d+)?/)[0];
if (hasClicked) {
chrome.tabs.create({ url: "https://securityheaders.io/?hide=on&source=chromeplugin&q=" + host });
}
else
{
hasClicked = true;
if(isValidURL(host)) {
xhr.open("HEAD", "https://securityheaders.io/?hide=on&source=chromeplugin&q=" + host, true);
xhr.send();
}
}
currentURL = tab.url;
currentTab = tab.id;
});
// Handle tab switching
chrome.tabs.onActivated.addListener(function ({ tabId }) {
setDefault();
currentTab = tabId;
chrome.tabs.get(tabId, function (tab) {
currentURL = tab.url;
});
});
// Handle URL changes
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if(tab.url != currentURL && tabId == currentTab) {
setDefault();
}
});
// Handle XMLHTTP State Changes
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
score = JSON.parse(atob((xhr.getResponseHeader('X-Score'))));
switch(score.score) {
case "A+":
chrome.browserAction.setIcon({path: 'icons/Aplus.png'});
break;
case "A":
chrome.browserAction.setIcon({path: 'icons/A.png'});
break;
case "B":
chrome.browserAction.setIcon({path: 'icons/B.png'});
break;
case "C":
chrome.browserAction.setIcon({path: 'icons/C.png'});
break;
case "D":
chrome.browserAction.setIcon({path: 'icons/D.png'});
break;
case "E":
chrome.browserAction.setIcon({path: 'icons/E.png'});
break;
case "F":
chrome.browserAction.setIcon({path: 'icons/F.png'});
break;
case "R":
chrome.browserAction.setIcon({path: 'icons/R.png'});
break;
case "Q": // error
chrome.browserAction.setIcon({path: 'icons/Q.png'});
break;
default:
// do nothing
break;
}
}
}
function isValidURL(url) {
return (url.indexOf("http://") == 0 || url.indexOf("https://") == 0) ? true : false;
}
function setDefault() {
chrome.browserAction.setIcon({path: 'icons/security-headers-icon-48.png'});
hasClicked = false;
}