Skip to content

Instantly share code, notes, and snippets.

@raf924
Created August 15, 2015 22:10
Show Gist options
  • Save raf924/ff824f7fac9673c67fc5 to your computer and use it in GitHub Desktop.
Save raf924/ff824f7fac9673c67fc5 to your computer and use it in GitHub Desktop.
Better autocompletion for hack.chat
var orig_keydown = $("#chatinput").onkeydown;
$("#chatinput").onkeydown = function(e){
if(e.keyCode==9){
e.preventDefault();
var pos = e.target.selectionStart || 0;
var text = e.target.value;
var index = text.lastIndexOf('@', pos);
if (index >= 0) {
var stub = text.substring(index + 1, pos);
// Search for nick beginning with stub
var nicks = onlineUsers.filter(function(nick) {
return nick.indexOf(stub) == 0;
});
if(nicks.length==0){
insertAtCursor(nicks[0].substr(stub.length));
}
insertAtCursor(nicks[0].substr(stub.length)+" ");
}
}
else{
orig_keydown(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment