forked from Onyx47/TDWTF-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
showstats.js
53 lines (52 loc) · 2.49 KB
/
showstats.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
// ==UserScript==
// @name TDWTF - Show user stats
// @match http://what.thedailywtf.com/*
// @namespace TDWTF
// @description Shows post count and unique badge count for users below their avatar
// @version 1
// ==/UserScript==
Ember.View.reopen({
didInsertElement: function () {
this._super();
Ember.run.scheduleOnce('afterRender', this, this.insertUserStats);
},
insertUserStats: function () {
}
});
Discourse.View.reopen({
insertUserStats: function () {
// This thing triggers for any render, so we check if a post has been rendered
if (this.post) {
// Get post ID
var postID = this.post.post_number;
// Get user name
var username = this.post.username;
// Initialize count variables
var postCount = 0;
var badgeCount = 0;
var userDataJsonUrl = 'http://what.thedailywtf.com/users/' + username + '.json';
$.get(userDataJsonUrl) .done(function (data) {
if (data.user) {
badgeCount = data.user.badge_count;
for (var i in data.user.stats) {
if (data.user.stats.hasOwnProperty(i)) {
// Security through obscurity!
if (data.user.stats[i].action_type === 5) {
postCount = data.user.stats[i].count;
}
}
}
$('.tdwtf-post-count[data-username="' + data.user.username + '"]') .html(postCount);
$('.tdwtf-badge-count[data-username="' + data.user.username + '"]') .html(badgeCount);
}
});
// Find avatar area for the post
var avatarArea = $('article[id="post_' + postID + '"]') .children('.row') .children('.topic-avatar');
// Check if the container has already been appended
if (avatarArea.children('.tdwtf-user-stats') .length == 0) {
// Append stats elements
avatarArea.append('<span class="tdwtf-user-stats" data-username="' + username + '" style="font: normal normal 400 10px Arial; color: #A7A7A7;"><i class="fa fa-envelope"></i> <span class="tdwtf-post-count" data-username="' + username + '">' + postCount + '</span><br><i class="fa fa-certificate"></i> <span class="tdwtf-badge-count" data-username="' + username + '">' + badgeCount + '</span>');
}
}
}
});