forked from jas-on/StackOverview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (52 loc) · 2.19 KB
/
index.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
(function() {
function checkPage(link) {
var req = new XMLHttpRequest();
req.onreadystatechange = function() {
//when we get a response
if (req.readyState === 4) {
var possibleAnswers = /(?:answerCount">)(.*?)</.exec(req.responseText);
var answerCount = possibleAnswers ? possibleAnswers[1] : 0;
//if there is at least one answer
var badgeClass;
if (answerCount > 0) {
//and one was accepted
if (req.responseText.indexOf("vote-accepted-on") !== -1) {
badgeClass = "badge badge-accepted";
} else {
//and none were accepted
badgeClass = "badge badge-normal";
}
} else {
//if there are no answers
badgeClass = "badge badge-none";
}
//create the badge
var badge = document.createElement("span");
badge.className = badgeClass;
badge.innerText = answerCount;
//insert the badge into the search entry
var target = link.parentNode.nextSibling.firstChild.firstChild;
target.insertBefore(badge, target.firstChild);
}
};
req.open('GET', link.mod_href, true);
req.send(null);
}
//determine if we're viewing google search as https
var isSecureSearch = window.location.origin.match(/^https/);
//get all of the search results
var links = document.getElementsByTagName("a");
for(var i = 0; i < links.length; ++i) {
//support StackOverflow and StackExchange
if (links[i].href.match(/http(s)?:\/\/[a-zA-Z.]*stackoverflow.com\/questions\/[0-9]*\//) ||
links[i].href.match(/http(s)?:\/\/[a-zA-Z.]*stackexchange.com\/questions\/[0-9]*\//)) {
var href = links[i].href;
//use https
if (isSecureSearch) {
href = links[i].href.replace(/http(?!s)/, "https");
}
links[i].mod_href = href;
checkPage(links[i]);
}
}
})();