-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
102 lines (85 loc) · 3.44 KB
/
content.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
98
99
100
101
102
links = document.getElementsByTagName('a');
for (i = 0; i < links.length; i++) { //for every link, create a mouseover event
links[i].onmouseover = function () {
//saves default style to oldStyle attribute
if (this.hasAttribute("oldStyle") == false){ //checks whether oldstyle attribute exists
var old = this.getAttribute("style");
this.setAttribute("oldStyle", old);
}
//console.log(this.href);
if (this.href != ""){
firstStatus = status1(this.href);
//when link response from status 1 is fast
if (firstStatus >= 200 && firstStatus <=399 ){
validLink(this);
}
//when link response from status 1 is slow / not complete
else if (firstStatus == 0){
status2(this);
}
//when status1 returns a bad response
else{ //console.log("else condition");
invalidLink(this);
}
}
}
}
function status1(url){ //checks the link status synchronously
var xhr = new XMLHttpRequest();
try {
xhr.open("GET", url, false);
xhr.send();
//console.log("status1(",url,")", xhr.status);
return xhr.status;
}
catch (err){
//console.log('status1 error:', url, xhr.status);
return xhr.status;
}
}
function status2(thisLink){ //checks the link status asynchronously when status1 gives response 0
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
//console.log('ASYNC:', thisLink.href, xhr.readyState, xhr.status);
if (xhr.readyState == 4){ //when url response is ready, check linkstatus
if (xhr.status >= 400 && xhr.status <=599) { // invalid link
validLink(thisLink);
}
else if (xhr.status != 0) {validLink(thisLink);}
//do nothing if xhr.status = 0
}
};
xhr.open("GET", thisLink.href, true);
xhr.send();
}
function validLink(thisLink) { //changes doc to show it is valid
chrome.storage.sync.get( //get greenBox and greenBoxTime values from popup saved to chrome.storage
['greenBox', 'greenBoxTime'], function(result) {
//console.log(result);
if (result.greenBox == true){
thisLink.style['background-color'] = "lightgreen";
thisLink.style.color = "black";
if (result.greenBoxTime == '2'){
setTimeout(function() {
thisLink.style = thisLink.getAttribute("oldStyle");
}, 2000);
}
else if (result.greenBoxTime == '5'){
setTimeout(function() {
thisLink.style = thisLink.getAttribute("oldStyle");
}, 5000);
}
}
});
}
function invalidLink(thisLink){ //changes doc to show link is invalid
chrome.storage.sync.get( //get unclickable and strikethrough values from popup saved to chrome.storage
['unclickable', 'strikethrough'], function(result) {
if (result.unclickable == true){
thisLink.removeAttribute("href"); //remove url
}
if (result.strikethrough == true){
thisLink.style.setProperty("text-decoration", "line-through"); //strikethrough link
}
});
}