-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
70 lines (60 loc) · 2.68 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
59
60
61
62
63
64
65
66
67
68
69
70
(function() {
function repeat() {
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");
var i, possibleBadge, href;
for(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]*\//)) {
possibleBadge = links[i].parentNode.nextSibling.firstChild.firstChild.firstChild;
if (possibleBadge.className.indexOf("badge") > -1) {
continue;
}
href = links[i].href;
if (isSecureSearch) {
href = links[i].href.replace(/http(?!s)/, "https");
}
links[i].mod_href = href;
checkPage(links[i]);
}
}
}
repeat();
setInterval(repeat, 2000);
})();