Add Unfollow Buttons to Twitter.com/home

// ==UserScript==
// @name           AddUnfollowButtons
// @namespace      KAtwitterScripts
// @description    Adds an 'unfollow' button to each tweet on twitter/home page
// @include        http://twitter.com/home
// ==/UserScript==

// This script will be of use to you iff you use Firefox,
// have Greasemonkey installed, and visit twitter.com/home.
// It is not going to work anywhere but on your twitter home page.

// Copyright 2009 by Kas Thomas
// Twitter: kasthomas
// Blog: http://asserttrue.blogspot.com/
// You may freely redistribute this script.
//
// Legal notice: Use this code at your own risk. Every
// disclaimer known to man applies.
//
// Tested in Firefox 3.0.3 on Vista.


// Our own wrapper around Greasemonkey's XHR object,
// instantiated as a singleton:

HttpPost = new function ( ) {
 
    // this is a public property
    this.header = {
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
        'Accept': 'application/atom+xml,application/xml,text/xml',
        };

    // call this method to set Basic Auth credentials:
    this.setCredentials =

        function( user,pass ) {
            var credentials = btoa( user + ":" + pass );
            this.header[ 'Authorization' ] = "Basic " + credentials;
        }

    this.invoke =
    function (  url, data, onloadCallback, onerrorCallback  ) {

        try {
            // do the AJAX POST
        GM_xmlhttpRequest ( {
            method: 'POST',
            url: url,
            data: data,
            headers: this.header,
            onload:  onloadCallback,
            onerror: onerrorCallback
        } );
        }
        catch( msg ) {  
            alert( " HttpPost.invoke( ) failed: " + msg );
            }
    }  // invoke
}

  // if error, write stuff to the Firefox error console
function _err( response ) {  

    if ( response.status > 201 ) {
        GM_log( "Status: " + response.status );
        GM_log( "Response text: " + response.responseText );
    }
}

   // this is the AJAX callback (duh)
function responseHandler( response ) {

    GM_log( "User unfollowed...  \n" +  response.responseText );
    location.reload( );  // reload page, your 'following' count should drop
}

   // this is the Unfollow Button's event handler
function clickHandler( e ) {

    var user = e.target.getUserData( "username" );
    var url = getQueryUrl( user );  

    HttpPost.setCredentials( "myname","mypassword" );  // USE YOUR USERNAME, PASSWORD
    HttpPost.invoke( url, "", responseHandler, _err );
}

   // create a "delete" URL per Twitter's API
function getQueryUrl ( who ) {

    var base = "http://twitter.com/friendships/destroy/";
    var url = base + who + ".xml"
    return url;
}

   // Reusable function, not Twitter-dependent
function createButton( label, style ) {

    var button = document.createElement( "input" );
    button.setAttribute( "type", "button" );
    button.setAttribute( "value", label );
    button.setAttribute( "style", style );
   
    return button;  // new placeless DOM node
}

function addButtonsToPage( ) {

        // This is our point of reference in the DOM for
        // obtaining the username, AND for finding the insertion point
        // for the button (both are relative to 'entries');
        // somewhat dangerous, obviously, because if the Twitter
        // folks ever redesign the home page, all bets are off:
    var entries =
        document.body.getElementsByClassName( "meta entry-meta" );

    for ( var i = 0; i < entries.length; i++ ) {

            // cheesy way of parsing out the username
        var username =
            entries[ i ].firstChild.getAttribute('href').split('/')[3];

            // create a new button
        var button = createButton( "unfollow" , "height:22px;" );

            // attach private data to it
        button.setUserData( "username" , username, new Function( ) );

            // this puts the button under the person's thumbnail
        entries[ i ].parentNode.parentNode.appendChild( button );

            // add the button's event handler
        button.addEventListener( "click" , clickHandler, false );
    }  
}


// ======================  main ( )  ====================
(   function main ( ) {

    addButtonsToPage( );

} ) ( );
 
Comments