Doing it wrong userscripts - User info


  • :belt_onion:

    Continuing the discussion from Doing it wrong userscripts - Show raw:

    Let's add some stuff to Discourse! And, if possible, stuff that is deemed as Doing it Wrong™

    Users need no stinkin' info! Info is a barrier to reading.

    So, time to add some: this time, post and unique badge counts below the avatars:

    Script:

    // ==UserScript==
    // @name        TDWTF - Show user stats
    // @namespace   TDWTF
    // @description Shows post count and unique badge count for users below their avatar
    // @version     1
    // @grant       none
    // ==/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>&nbsp;<span class="tdwtf-post-count" data-username="' + username + '">' + postCount + '</span><br><i class="fa fa-certificate"></i>&nbsp;<span class="tdwtf-badge-count" data-username="' + username + '">' + badgeCount + '</span>');
                }
            }
        }
    });
    
    
    

    Taking suggestions for reasonable caching solution. This version will update live on every new post, but the amount of requests might be a bit too much. I didn't notice it clobbering my connection much but YMMV.

    Suggestions, bug reports, fixes and shit can go below. Github repo is possible if a need arises. Or just stick it up there yourself, I don't care. Released under WTFPL.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.