Created
August 12, 2008 15:47
-
-
Save mcfearsome/5063 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Tab Completion for Campfire | |
// @namespace http://code.cimians.com | |
// @description If you hit tab in the campfire chat window, it will tab complete on user names. | |
// @author Mat Schaffer, CIM Engineering | |
// @homepage http://code.cimians.com | |
// @include *.campfirenow.com/room* | |
// ==/UserScript== | |
(function() { | |
var getSelection = function() { | |
var start = $('input').selectionStart; | |
var end = $('input').selectionEnd; | |
var selection = $('input').value.substring(start, end); | |
return selection; | |
} | |
var setName = function(stub, name, doSelect) { | |
var ss = $('input').value.length; | |
var rep = new RegExp(stub+'$','i'); | |
var input = $('input').value; | |
$('input').value = input.replace(rep, name); | |
if(!doSelect) { | |
$('input').value += " "; | |
var ss = $('input').value.length; | |
} | |
var se = $('input').value.length; | |
$('input').setSelectionRange(ss,se); | |
} | |
Event.observe('input', 'keypress', function(event) { | |
var input = $("input").value; | |
if(event.keyCode == 32 && window.tc_matches && window.tc_matches.length) { | |
$('input').value += ' '; | |
var l = $('input').value.length; | |
$('input').setSelectionRange(l,l); | |
delete window.tc_matches; | |
delete window.tc_stub; | |
delete window.tc_index; | |
}else if (event.keyCode == 9) { | |
if(window.tc_matches && window.tc_matches.length > 0) { | |
var l = window.tc_matches.length; | |
var index = window.tc_index + 1; | |
if(index >= l) { | |
index = 0; | |
} | |
var name = window.tc_matches[index]; | |
var s = getSelection(); | |
var sr = new RegExp(s+'$','i'); | |
$('input').value = input.replace(sr,''); | |
setName(window.tc_stub, name, true); | |
window.tc_index = index; | |
return false; | |
} | |
var stub = $A(input.split(" ")).last(); | |
var selection_start = input.length; | |
var regexp = new RegExp('^'+stub,'i'); | |
var matches = []; | |
$$('.user .name').each(function(user) { | |
var name = user.innerHTML; | |
// Right now it will use the first match it finds | |
// TODO :: keep track of matches and if more than one provide a way to select the right one. | |
if(name.match(regexp)) { | |
matches.push(name); | |
} | |
}); | |
if(matches.length > 1) { | |
window.tc_matches = matches; | |
window.tc_stub = stub; | |
window.tc_index = 0; | |
setName(stub, matches[0], true); | |
} else if(matches.length == 1) { | |
setName(stub, matches[0], false); | |
} | |
$('input').focus(); | |
} else if(window.tc_matches && window.tc_matches.length) { | |
delete window.tc_matches; | |
delete window.tc_stub; | |
delete window.tc_index; | |
} | |
}) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment