forked from PageModifiedOfficial/Check-My-Links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.js
executable file
·172 lines (138 loc) · 8.86 KB
/
check.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Check My Links by Paul Livingstone
// @ocodia
String.prototype.startsWith = function(text) {
return this.substr(0, text.length) == text;
};
String.prototype.contains = function(text) {
return this.indexOf(text) !== -1;
};
chrome.extension.onMessage.addListener(
function(request, sender) {
// Gather links
var pageLinks = document.getElementsByTagName('a');
var totalvalid = pageLinks.length;
var queued = 0;
var checked = 0;
var invalid = 0;
var passed = 0;
var rpBox;
(
function(pg){
var blacklist = request.bl;
blacklist = blacklist.split("\n");
var blacklisted;
// Inject Styles and Elements for feedback report
var reportStyle = document.createElement("style");
reportStyle.setAttribute("rel", "stylesheet");
reportStyle.setAttribute("type", "text/css");
document.getElementsByTagName("head")[0].appendChild(reportStyle);
reportStyle.appendChild(document.createTextNode(".CMY_Link{background: #333 !important; color: #fff !important; font-weight: bold; border-right: 1px solid #fff;}"));
reportStyle.appendChild(document.createTextNode(".CMY_Invalid{background: #990000 !important; color: #fff !important;}"));
reportStyle.appendChild(document.createTextNode(".CMY_Valid{background: #669900 !important; color: #fff !important;}"));
reportStyle.appendChild(document.createTextNode(".CMY_Response{color:#fff; border-left: 1px solid #fff; padding: 0px 5px; margin: 0px 0px 0px 5px;}"));
reportStyle.appendChild(document.createTextNode("#CMY_ReportBox{font-weight: bold; width: 180px; position: fixed; right:0px; top: 0px; background: #fff; margin: 20px; padding: 0px; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 14px; border-radius: 5px; z-index: 9999; box-shadow: 0px 0px 3px(0,0,0,0.2);}"));
reportStyle.appendChild(document.createTextNode("#CMY_RB_Header{border-radius: 5px 5px 0px 0px; border-bottom: 1px solid #444; background: -webkit-gradient(linear, center top, center bottom, from(#777), to(#3B3B3B)); text-align: center; color: #000; font-size: 12px; padding: 5px; margin: 0px; text-shadow: 1px 1px 1px #666;}"));
reportStyle.appendChild(document.createTextNode("#CMY_ReportBox .CMY_RB_ResultCount{font-size: 14px; box-sizing: border-box; width: 50%; color: #fff; font-weight: bold; line-height: 14px; text-shadow: 0px 0px 4px rgba(0,0,0,0.3); text-align: left; padding: 10px 10px 10px 40px; background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAABQCAYAAAD2gj5JAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAR2SURBVHja7JjdaxxVGMbPObM7HzszuwkoVdv+DSJCK2oilaz1o4KBJqYVTW+0ou2NjZZaQ2vTIoGoTa010gixjbtJm1KF9qZ3ghdeFgreeKeIopjd7Mxukt3sOT5nsklnZ2aTGPVCmIHDZnfe85vnvO/znjCHCiHIv33RGLrpyV29dp+SJPlEgsqv3Te/LH39j6ASmNDIZdNmCd1k8qfqvMv3VRx+c1NQAHsBnAKQ2m0KUQ1PKVmc5/NukWcTm1T4lR/IPKGkrmr0mGawOyFo14v2IFPIlls551AL4CSALADknJOBWpWcy31YEE3LB/AdPG1YNymhlHx67cLcocCS8xFAITh5a3FBnJs4M8ubCgXguwCeMTOMmGkmoQRJPz89Wjyc7bN7lARymA4rBPAIgJ8AWA9ZavdL9s9WG9uWbsckbTnxtaog5RL/bqEidiRVolqZMPDzwT/PBtPEVum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AD2yuPPDbbSegAAAABJRU5ErkJggg%3D%3D); background-repeat: no-repeat;}"));
reportStyle.appendChild(document.createTextNode("#CMY_ReportBox .CMY_RB_LinkCounts{text-align: center; background: #111; font-size: 11px; font-weight: bold; padding: 0px 0px 10px 0px; box-sizing: border-box; color: #fff; line-height: 12px; width: 50%;}"));
reportStyle.appendChild(document.createTextNode("#CMY_ReportBox #CMY_RB_LC_Left{float: left;}"));
reportStyle.appendChild(document.createTextNode("#CMY_ReportBox #CMY_RB_LC_Right{float: right; color: #f59703;}"));
reportStyle.appendChild(document.createTextNode("#CMY_ReportBox #CMY_RB_Perc{font-size: 50px; line-height: 50px; color:#fff; background: #111; padding: 10px; text-shadow: 0px 0px 5px rgba(255,255,255,0.8); text-align: center;}"));
reportStyle.appendChild(document.createTextNode("#CMY_ReportBox #CMY_RB_Pass{border-top: 1px solid #bddf5d; border-right: 1px solid #8db615; float:left; border-radius: 0px 0px 0px 5px; background-color:#9fcd19; background-position-x: 8px; background-position-y: 5px;}"));
reportStyle.appendChild(document.createTextNode("#CMY_ReportBox #CMY_RB_Fail{border-top: 1px solid #c54f4f; float: right; border-radius: 0px 0px 5px 0px; background-color: #a9060a; background-position-x: 8px; background-position-y: -52px;}"));
var reportBox = document.createElement("div");
var rbHeader = document.createElement("div");
var rbPerc = document.createElement("div");
var rbAmt = document.createElement("div");
var rbQueue = document.createElement("div");
var rbPass = document.createElement("div");
var rbFail = document.createElement("div");
reportBox.setAttribute("id", "CMY_ReportBox");
rbHeader.setAttribute("id", "CMY_RB_Header");
rbHeader.innerHTML = "Link Results";
rbPerc.setAttribute("id", "CMY_RB_Perc");
rbAmt.setAttribute("id", "CMY_RB_LC_Left");
rbAmt.setAttribute("class", "CMY_RB_LinkCounts");
rbQueue.setAttribute("id", "CMY_RB_LC_Right");
rbQueue.setAttribute("class", "CMY_RB_LinkCounts");
rbPass.setAttribute("id", "CMY_RB_Pass");
rbPass.setAttribute("class", "CMY_RB_ResultCount");
rbFail.setAttribute("id", "CMY_RB_Fail");
rbFail.setAttribute("class", "CMY_RB_ResultCount");
document.getElementsByTagName("body")[0].appendChild(reportBox);
rpBox = document.getElementById("CMY_ReportBox");
rpBox.appendChild(rbHeader);
rpBox.appendChild(rbPerc);
rpBox.appendChild(rbAmt);
rpBox.appendChild(rbQueue);
rpBox.appendChild(rbPass);
rpBox.appendChild(rbFail);
rpBoxPerc = document.getElementById("CMY_RB_Perc");
rpBoxAmt = document.getElementById("CMY_RB_LC_Left");
rpBoxQueue = document.getElementById("CMY_RB_LC_Right");
rpBoxPass = document.getElementById("CMY_RB_Pass");
rpBoxFail = document.getElementById("CMY_RB_Fail");
rpBoxPerc.innerHTML = "0%";
rpBoxAmt.innerHTML = "Links: 0";
rpBoxQueue.innerHTML = "Queue: 0";
rpBoxPass.innerHTML = "0";
rpBoxFail.innerHTML = "0";
// Run through links, add feedback classes and ignore empty href and non http* links
for (var i = 0; i < pg.length; i++){
var link = pg[i];
var url = link.href;
var rel = link.rel;
blacklisted = false;
if (url.length > 0 && url.startsWith('http') && rel !== "nofollow"){
for (var b = 0; b < blacklist.length; b++)
{
if (blacklist[b] !== "" && url.contains(blacklist[b])){
blacklisted = true;
}
}
if (blacklisted === true){
console.log("Skipped (blacklisted): " + url);
totalvalid -=1;
}
else{
queued +=1;
link.classList.add("CMY_Link");
checkURL(url, link);
//console.log("Checking:" + url);
}
}
else{
console.log("Skipped: " + url);
totalvalid -=1;
}
}
rpBoxAmt.innerHTML = "Links: " + totalvalid;
}(pageLinks)
)
// Send links to get checked via XHR
function checkURL(url, link) {
chrome.extension.sendMessage({"action": "check", "url": url},
function (response) {
if (response) {
if (200 <= response && response < 400) {
link.classList.add("CMY_Valid");
queued -=1;
passed +=1;
checked +=1;
rpBoxPass.innerHTML = passed;
}
else {
console.log("Response " + response + ": " + url);
link.classList.add("CMY_Invalid");
link.innerHTML += " <span class='CMY_Response'>" + response + "</span>";
queued -=1;
invalid +=1;
checked += 1;
rpBoxFail.innerHTML = invalid;
}
rpBoxPerc.innerHTML = Math.round((checked)/totalvalid * 100) + "%";
rpBoxQueue.innerHTML = "Queue: " + queued;
}
});
}
return true;
});