Facebook - Poke All

By Jack Hsu Last update 2 hours ago — Installed 4,269 times. Daily Installs: 12, 24, 15, 21, 8, 16, 16, 8, 15, 11, 10, 9, 10, 17, 11, 9, 5, 9, 17, 6, 6, 4, 4, 12, 6, 8, 3, 7, 9, 5, 6, 2

There are 8 previous versions of this script.

// ==UserScript==
// @name           facebook poke all
// @namespace      facebook
// @include     http://facebook.com/home.php*
// @include     http://*.facebook.com/home.php*
// ==/UserScript==

// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://code.jquery.com/jquery-latest.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

// Check if jQuery's loaded
function GM_wait() {
    if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
else { unsafeWindow.jQuery.noConflict(); letsJQuery(); }
}
GM_wait();

// All your GM code must be inside this function
function letsJQuery() {
	var $ = unsafeWindow.jQuery;
	var box = $("#home_sidebar div.sidebar_item.pokes");
	var pokes = $("div.ind_poke", box);
	var _regex = /^.+"onLoad"\s*:\s*\["(.+)"\].+$/i;

	if ( pokes.length > 0 ) {
		// Message and button to add to the Pokes sidebar element
		var msg = $("<p/>");
		var pokeAllBtn = $("<input/>");
		var pokeSelectedBtn = $("<input/>");
		
		// Keep track of all pokees
		var unpoked = {};
		var total = 0;
		
		var idRegex = /poke_dialog\.php\?uid=(\d+)(&.*)?/;
		
		pokes.each(function(i, a) {
			var url = $("a", a).get(1).href;
			var userLink = $("a:first", a).get(0);
			var pokee = $(userLink).text();
			var id = idRegex.exec(url)[1];
			
			unpoked[id] = { div: a, pokeUrl: url + '&__d=1&__a=1', name: pokee, pageUrl: userLink.href };
			++total;
		});
		
		// Set up Buttons
		pokeAllBtn.attr("value","Poke All!").attr("type","button").addClass("inputsubmit").css( {
			"margin-top": "3px",
			padding: "2px 5px 3px"
		}).attr("id","pokeAllButton").click( function () {
			var pokees = [];
			
			// Disable the button
			$("#pokeAllButton").attr("disabled","disabled");
			
			// Loop through each pokee and poke them!
			$("div.ind_poke", box).each(function(i, a) {
    			var url = $("a", a).get(1).href;
    			var id = idRegex.exec(url)[1];

                pokees.push(doPoke(id));

                if (--total === 0) finalize();
			});
				
			confirmPokes(pokees.join(", "));;
		} );
		
		pokeSelectedBtn.attr("value","Poke Selected!").attr("type","button").addClass("inputsubmit").css( {
			"margin-top": "3px",
			padding: "2px 5px 3px",
			"margin-left": "3px"
		} ).attr("id","pokeAllButton").click( function () {
			var toPoke = $(".pokeCheckbox:checked");
			
			if (toPoke.length === 0) {
				alert("You haven't selected users to poke!");
                return;
            }

            // Keep track of all pokees
            var pokees = [];
            
            $(toPoke).each(function( i, a ) {
                var id = $(a).attr("value");
                pokees.push(doPoke(id));
                if (--total === 0) finalize();
            });

			confirmPokes(pokees.join(", "));;
		} );
		
        // Add top message
		msg.html("<p>Do you want to poke everyone?</p>").css( {
			padding: "0 10px"
		} ).append(pokeAllBtn).append(pokeSelectedBtn).attr("id","pokeAll");

		// Add checkbox to each pokee
		pokes.each( function(i, a) {
			var url = $("a", a).get(1).href;
			var userLink = $("a:first", a).get(0);
			var pokee = $(userLink).text();
			var id = idRegex.exec(url)[1];
			
			var checkBox = $("<div>");
			checkBox.attr("id",id+"Poke").css( { 
				display: "inline"
			} ).html('<input style="vertical-align: middle" value="'+id+'" type="checkbox" id="'+id+'" class="pokeCheckbox" /><label title="Include '+pokee+' in Poke Selected!" style="font-size: 9px; font-weight: normal" for="'+id+'">?</label>');
			
			$(a).css( { float: "left", display: "block" } ).append(checkBox);
		} );
		
		$("div.UITitledBox_Top", box).after(msg);
		
		// Show results
		var results = $("<p/>");
		results.css( {
			padding: "0 10px",
			color: "#666666",
			display: "none",
			margin: "5px",
			"background-color": "#E9E9E9",
			border: "1px solid #EEEEEE"
		} ).attr("id","pokeConfirm");
		$("#pokeAll").after(results);
	} 

    function doPoke(id) {
        var user = unpoked[id];
        
        // Add pokee to the array
        var poked = '<a href="'+user.pageUrl+'">'+user.name+'</a>';
        
        // We'll send a request for the facebook poke page
        $.ajax( {
            type: "GET",
            url: user.pokeUrl,
            success: function(data) {
                data = data.match(_regex)[1].replace(/\\|;/g, '');
                setTimeout(function() {
                    var args = eval('[' + data.match(/\((.+)\)/)[1] + ']');
                    var controller = new this.PokeController(
                        args[0], args[1], args[2], args[3], args[4], args[5]
                    );
                    controller.poke();
                }, 50);
            }
         } );
         
         $(user.div).slideUp().remove();
         return poked;
    }

    function confirmPokes(msg) {
        $("#pokeConfirm").slideUp().html("You have just poked the following users.<br/><br/>"+msg).slideDown();
    }

    function finalize() {
        // Show the close option for Pokes
        var closepokeAllBtn = document.createElement("div");
        var closeLink = document.createElement("a");
        
        $(closeLink).attr("href","#").text("close").click( function () {
            $(".sidebar_item.pokes").slideUp();
            return false;
        } );
        
        $(closepokeAllBtn).addClass("option").append(closeLink).css({'float': 'right'});
        
        box.append(closepokeAllBtn);				
        
        $("div.UIPokes", box).text("No one left to poke.");
        
        // Hide poke all
        $("#pokeAll").slideUp();
    }
}