Facebook - Poke All

By Jack Hsu Last update Sep 4, 2009 — Installed 4,143 times. Daily Installs: 9, 9, 8, 10, 16, 9, 14, 10, 9, 11, 10, 9, 8, 9, 5, 7, 7, 12, 24, 15, 21, 8, 16, 16, 8, 15, 11, 10, 9, 10, 17, 11

There are 7 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);

	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, 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(html) { // Let's parse the response!
                var formIdRegex = /name="post_form_id" value="(\w+)"/;
                var formId = formIdRegex.exec(html)[1];
                
                // Now we can sent a POST request to actually poke the pokee
                $.ajax({
                    type: "POST",
                    url: "poke.php",
                    data: "post_form_id=" + formId + "&id=" + id + "&confirmed=1&pokeback=1"
                });
            }
         } );
         
         $(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();
    }
}