-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop-writers-followers-counts.js
80 lines (57 loc) · 1.9 KB
/
top-writers-followers-counts.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
(function(){
var followers = {}, // will contains followers counts
// this is used to get the followers count on a profile
re = /Followers\s*<span class="[^"]+">(\d+)<\/span>/m,
// this is a list of all top writers (<a> elements)
$quorans = $( ".wiki_text span.qlink_container a" ),
count = $quorans.length;
// this will display the list
function makeString() {
var s = "",
// list of all names
top = Object.keys(followers),
q;
// sort the list by followers counts
top = top.sort(function(a,b) {
return followers[b].followers - followers[a].followers;
});
// for each Top Writer, …
for (var i=0, l=top.length; i<l; i++) {
q = followers[top[i]];
// add a link to their profile
top[i] = '<a href="'+q.url+'">'+top[i]+"</a> ("+q.followers+")";
}
s = top.join("<br />");
// display the list. This is ugly, but the goal is to save time,
// not to make a beautiful page.
document.body.innerHTML = s;
}
// for each Top Writer, do…
$quorans.each( function( i,a ) {
var url = a.href, // pick their profile URL
name = a.textContent; // and their name
// go on their profile
$.ajax( url, {
success: function( html ) {
followers[name] = {
// get their followers count
followers: +(re.exec(html)[1]),
// keep their profile URL
url: url
};
// 499, 498, …, 1
console.log(count);
// done? Let’s display the list!
if (--count === 0) {
makeString();
}
},
// if there is an error, you have to re-run the script,
// or check the followers count by yourself
error: function() {
count--;
console.log("Error with "+name+", skipping");
}
});
});
})();