Squirrelmail AutoComplete fix

By Rockmaster Last update Oct 2, 2009 — Installed 53 times.
// ==UserScript==
// @name           Squirrelmail AutoComplete fix
// @namespace      *
// @description    Makes email address autocomplete work with Firefox
// @include        https://webmail.drizzle.com/src/compose.php*
// ==/UserScript==

// hacked together by Rockmaster 
// based off of code by mo /mo@momche.net/ from moautocomplete.js
// written to work with SquirrelMail with AutoComplete plug-in 
//     http://www.squirrelmail.org/plugin_view.php?id=32

// This script uses the array "autocompleteArray" generated by the AutoComplete plugin
// and Mo's code which creates a new class for input fields.
// The original To: input field:
// <input type="text" name="send_to" value="" size="60"  onfocus="alreadyFocused=true;" 
// is changed to this:
// <input type="text" name="send_to" value="" size="60" onfocus="alreadyFocused=true;" autocomplete="array:autocompleteArray">


function replaceToField() {
	allSendToFields = document.getElementsByName('send_to');
	for (var i = 0; i < allSendToFields.length; i++) {
		thisSendToField = allSendToFields[i];
		// do something to insert autocomplete="array:autocompleteArray"
		thisSendToField.innerHTML = '<input type="text" name="send_to" value="" size="40 !important" onfocus="alreadyFocused=true;" autocomplete="array:autocompleteArray">';
	alert("hi!",i);
	}
}


//  Mo's code with disclaimer:
//  This script was created
//  by Mircho Mirev
//  mo /mo@momche.net/
//
//	:: feel free to use it BUT
//	:: if you want to use this code PLEASE send me a note
//	:: and please keep this disclaimer intact

var cAutocomplete =
{
	sDescription : 'autcomplete class'
}


cAutocomplete.complete = function( hEvent )
{
	if( hEvent == null )
	{
		var hEvent = window.hEvent
	}

	var hElement = ( hEvent.srcElement ) ? hEvent.srcElement : hEvent.originalTarget
	
	var sAA = hElement.getAttribute( 'autocomplete' ).toString()
	if( sAA.indexOf( 'array:' ) >= 0 )
	{
		hArr = eval( sAA.substring( 6 ) )
	}
	else if(  sAA.indexOf( 'list:' ) >= 0 )
	{
		hArr = sAA.substring( 5 ).split( '|' )
	}
	


	if( hEvent.keyCode == 16 )  
	{
		return
	}
	var sVal = hElement.value.toLowerCase()
	if( hEvent.keyCode == 8 )
	{
		sVal = sVal.substring( 0, sVal.length - 1 )
	}
	if( sVal.length < 1 )
	{
		return
	}
	for( var nI = 0; nI < hArr.length; nI++ )
	{
		sMonth = hArr[ nI ]
		nIdx = sMonth.toLowerCase().indexOf( sVal, 0 )
		if( nIdx == 0 && sMonth.length > sVal.length )
		{
			hElement.value = hArr[ nI ]
			if( hElement.createTextRange )
			{
				hRange = hElement.createTextRange()
				hRange.findText( hArr[ nI ].substr( sVal.length ) )
				hRange.select()
			}
			else
			{
				hElement.setSelectionRange( sVal.length, sMonth.length )
			}
			return
		}
	}
}

cAutocomplete.init = function()
{

	replaceToField();  //insert code by Rockmaster

	var nI = 0
	//var aInputs = document.getElementsByTagName( 'INPUT' )  // modified by Rockmaster
	var aInputs = document.getElementsByName( 'send_to' )  // modified by Rockmaster
	for( var nI = 0; nI < aInputs.length; nI ++ )
	{
		if( aInputs[ nI ].type.toLowerCase() == 'text' )
		{
		 	var sLangAtt = aInputs[ nI ].getAttribute( 'autocomplete' )
			if( sLangAtt )
			{
					if( document.attachEvent ) 
					{
						aInputs[ nI ].attachEvent( 'onkeyup', cAutocomplete.complete )
					}
					else if( document.addEventListener )
					{
						aInputs[ nI ].addEventListener( 'keyup', cAutocomplete.complete, false )
					} 
			}
		}
	}
}

if( window.attachEvent ) 
{
	window.attachEvent( 'onload', cAutocomplete.init )
}
else if( window.addEventListener )
{
	window.addEventListener( 'load', cAutocomplete.init, false )
}