Tuesday, June 30, 2009

Twitter Greasemonkey Following Script

Today Twitter rolled out some changes to the user interface on the “Following” and “Followers” sections of their website.

The following code is a Firefox Greasemonkey script for the new UI that identifies on the "Following" section, whether Twitter users you follow are following you in return.

For example, in the screen capture below it's easy to see that @lancearmstrong isn't following me yet.

Twitter friends

Click here to install the script (on Firefox with the Greasemonkey add-on installed), or save the code below as a text file named "twitterfriendsfollowing.user.js", and then drag the file onto your Firefox browser.

Update: Twitter updated the class name 'direct-message-able' to 'direct-messageable', which has been changed in the code below and downloadable script (above).

// ==UserScript==
// @name           Twitter Friends Following
// @namespace      greasemonkey.hubkey.com
// @description    Identifies whether Twitter users you follow are following you in return.
// @include        http://twitter.com/*following*
// @include        https://twitter.com/*following*
// @include        http://twitter.com/*friends*
// @include        https://twitter.com/*friends*
// ==/UserScript==
 
twitter_friends_following = {
    count: 0,
 
    identify: function() {
        var afollowers = document.getElementsByClassName('direct-messageable');
        for (var i = 0; i < afollowers.length; i++) {
            var avcard = afollowers[i].getElementsByClassName('about vcard');
            if (avcard.length != 1)
                continue;
            var node = document.createElement('span');
            node.setAttribute('class', 'is-following');
            node.innerHTML = '<i></i><strong>Following</strong>';
            avcard[0].appendChild(node);
        }
 
        try {
            twitter_friends_following.count = document.getElementsByClassName('direct-messageable').length;
        } catch (e) {
            twitter_friends_following.count = 0;
        }
    },
 
    monitor: function() {
        if (document.getElementsByClassName('direct-messageable').length != twitter_friends_following.count)
            twitter_friends_following.identify();
 
        setTimeout(twitter_friends_following.monitor, 200);
    }
};
 
setTimeout(twitter_friends_following.monitor, 200);