Twitter Autocomplete

Auto Complete your friends' ID's in the Tweet Box

Begin by typing an '@' symbol below followed by a letter. Both space and enter will commit to the selected name.

The following names are available
  • adam
  • bilny08
  • colber87
  • caddy9
  • demimore
  • donj23
  • eddi85
  • emilee4
  • fabi
  • fenndi
  • glamm2
  • george
  • harryp24
  • hemma
  • ingrid
  • illini2
  • jonni5
  • jan
  • kerr
  • kandi
  • lawrence
  • lulu
  • mauri
  • manny
  • marry
  • nani
  • nano
  • opie
  • ontar22
  • pele75
  • pablo
  • quazar
  • quux
  • randyj00
  • riley
  • sandi
  • telmerfudd
  • tammi
  • ubermench
  • ursula
  • valor98
  • vince
  • vicky2
  • will.i.am
  • woman
  • xavier
  • xartha
  • yanni
  • yasmine
  • zeina
  • zorak

Why?

Auto Complete has been around for a while and as a quick hack I figured I'd just write one of my own in the context of twitter. I don't really use twitter all that much now but I figured a good way to change that is to make tweeting and replying to people faster. Since I don't always want to type in someones handle and sometimes my bad spelling gets me in trouble I wrote a script which would help me with that.

How it works

The script only recognizes the '@' token to begin a dictionary lookup.For this example the dictionary is a hardcoded array but this can easily be modified to be accepted as a parameter. The initial step then is to sort it and create an object indexed by the first letter of each name. For example:

	{a: ['andrew','amy','anna'], b: [...], etc.]}

Once that is done I am able to loop through a far fewer subset of the dictionary and once a token is entered I can begin deducing which name you are trying to type by doing a simple /^query/ regular expression.

Then the fun part starts; locating where to start selecting and, to boot it works differently in IE than it does in the rest of the world.

Luckily, however, this isn't too difficult either. Firstly detect the token then take the current text value starting from the index of that token and pass it through the simple reduction algorithm that then returns the first closest match.

The next step is setting the selection. The selection is based on the typed text plus the remainder of the best guess. This gives me the text to be selected. Using the length of the best guess I can form a remainder. With this remainder I can now do my selection starting from the position of the last character typed.

The current implementation requires a greasemonkey script. But maybe twitter will actually implement a feature like this.

You can view the script here

All of this is also available in the resources section