GMail toggle searchbar area

By Gurjeet Singh Last update Nov 13, 2009 — Installed 4,021 times. Daily Installs: 20, 10, 17, 112, 29, 20, 17, 12, 13, 19, 10, 14, 18, 10, 11, 12, 17, 6, 26, 29, 23, 29, 25, 30, 24, 31, 22, 21, 34, 27, 31, 22

There are 6 previous versions of this script.

// ==UserScript==
// @name           GMail toggle searchbar
// @namespace      http://gurjeet.singh.im
// @include        http://mail.google.com/mail/*
// @include        https://mail.google.com/mail/*
// @include        http://mail.google.com/a/*
// @include        https://mail.google.com/a/*
// ==/UserScript==
//

var script_node_id = 'toggle_search_script_node';

function my_log(message)
{
//  GM_log( message );
};

// All your GM code must be inside this function
function add_content()
{
	areaLocator = '.aC .nH .nH .no .nH .nH .no';
	barLocator = '.aC .nH .nH .qp #gbar';
	showText = 'Toggle Search';
	hideText = 'Toggle Search '; // Notice the extra space at the end
	button = '<a href="" onclick="return false" id="toggleSearch" class="gb1 qq">' + showText + '</a>';

	my_log( 'Entering add_content()' );

	old = $(barLocator).attr('innerHTML');

	if( typeof old == 'undefined' )
	{
		my_log( 'barLocator is undefined; retrying' );
		window.setTimeout( add_content, 1000);
		return;
	}

	// Add the text to existing row of links
	my_log( 'Adding button' );
	$(barLocator).attr("innerHTML", old + button);

	// Make the text/button clickable
	GM_addStyle("#toggleSearch:hover { cursor:pointer; }");

	// Hide the area by default
	my_log( 'Hiding the search area' );
	$(areaLocator).slideUp('slow');

	$('#toggleSearch').click( function()
	{
		if (this.innerHTML == showText)
		{
			$(areaLocator).slideDown('slow');
			this.innerHTML = hideText;
			$(document.getElementById(':ra')).focus();// $('#:ra') isn't working!
		}
		else
		{
			$(areaLocator).slideUp('slow');
			this.innerHTML = showText;
		}
	}        
	);

//	TODO: Implement detection of user hitting '/' to focus the search box
//	$(document.getElementById(':ra')).focus( function(){ $('#togglesearch').click(); } );

	document.getElementById( script_node_id )
		.parentNode.removeChild( document.getElementById( script_node_id ) );
}

// Check if jQuery is loaded
function wait_for_jquery()
{
	if(typeof unsafeWindow.jQuery == 'undefined')
	{
		my_log( 'Sleeping in wait_for_jquery()' );
		window.setTimeout(wait_for_jquery,1000); 
	}
	else
	{
		my_log( 'Waking up' );
		$ = unsafeWindow.jQuery;
		add_content();
	}
}

function wait_for_iframe()
{
	if( document.body.className != 'cP' )
	{
		if( GM_getValue( 'iframe_identified', 0 ) == 1 )
		{
			my_log( 'Stopping others' );
			return;
		}
	
		my_log( 'Sleeping in wait_for_iframe()' );
		window.setTimeout(wait_for_iframe,1000);
		return;
	}

	var my_JQ = document.createElement('script');  

	my_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';  
	my_JQ.type = 'text/javascript';
	my_JQ.id = script_node_id;
	document.getElementsByTagName('head')[0].appendChild(my_JQ);  
	
	GM_setValue( 'iframe_identified', 1 );

	wait_for_jquery();
		
	return;
}

GM_setValue( 'iframe_identified', 0 );
wait_for_iframe();