Username-completion

Tab cycles through users starting with characters

  1. // ==UserScript==
  2. // @name Username-completion
  3. // @namespace tag://kongregate
  4. // @description Tab cycles through users starting with characters
  5. // @include http://www.kongregate.com/games/*
  6. // @author Ventero
  7. // @date 03.01.2012
  8. // @version 1.5
  9. // require http://kong.ventero.de/updates/49872.js
  10. // ==/UserScript==
  11.  
  12. // Written by Ventero (http://www.kongregate.com/accounts/Ventero) 05/23/09
  13. // Thanks to kaedenn for the idea of adding a colon if the username is the first word in the message
  14. // Copyright (c) 2009-2012 Ventero, licensed under MIT/X11 license
  15. // http://www.opensource.org/licenses/mit-license.php
  16.  
  17. var s = document.createElement("script");
  18. s.textContent = "(" +
  19. function init_nameCompletion(){
  20. if(typeof ChatDialogue === "undefined" ||
  21. ChatDialogue.prototype.oldKeyPressTab) return;
  22.  
  23. var isChrome = (navigator.appVersion.indexOf("Chrome") >= 0);
  24. if(isChrome) {
  25. ChatDialogue.prototype.initialize =
  26. ChatDialogue.prototype.initialize.wrap(function(old, p, i, h, u){
  27. old(p, i, h, u);
  28. var self = this;
  29. this._input_node.observe("keydown", function(event) {
  30. if(event.keyCode != 9 || event.ctrlKey || event.altKey || event.metaKey) return;
  31. self.onKeyPress(event);
  32. });
  33. })
  34. }
  35.  
  36. ChatDialogue.prototype.oldKeyPressTab = ChatDialogue.prototype.onKeyPress;
  37. ChatDialogue.prototype.tabcnt = 0;
  38. ChatDialogue.prototype.done = 1;
  39. ChatDialogue.prototype.onKeyPress = function(a){
  40. if (a.keyCode != 9 || a.ctrlKey){
  41. this.tabcnt = 0;
  42. this.done = 1;
  43. this.oldKeyPressTab(a);
  44. return;
  45. }
  46.  
  47. var node = (this._input_node.wrappedJSObject || this._input_node);
  48. if (this.tabcnt == 0 && this.done == 1){
  49. var inputText = node.getValue(),
  50. spaceAtCaret = inputText.substr(0, node.selectionStart).lastIndexOf(' ');
  51. this._caretPos = node.selectionStart;
  52. this._start = inputText.substr(0,spaceAtCaret);
  53. if(this._start) this._start+=" ";
  54.  
  55. this._currentWord = inputText.substring(spaceAtCaret+1, this._caretPos);
  56. this._rest = inputText.substr(this._caretPos);
  57. }
  58. this.done = 0;
  59.  
  60. var userArray = this._holodeck.chatWindow().activeRoom()._users_list,
  61. possibleMatches = [],
  62. z = node.getValue();
  63. if (z.match(/\s+$/)) z=z.replace(/\s+$/, '')
  64.  
  65. for (var i=0;i<userArray.length;i++){
  66. if(userArray[i].username.toLowerCase().indexOf(this._currentWord.toLowerCase())==0){
  67. possibleMatches.push(userArray[i].username);
  68. }
  69. }
  70.  
  71. if (this.tabcnt < possibleMatches.length){
  72. node.setValue(this._start + possibleMatches[this.tabcnt] + (this._start?" ":": ") + this._rest);
  73. node.selectionStart = this._caretPos + possibleMatches[this.tabcnt].length - this._currentWord.length+(this._start?1:2);
  74. node.selectionEnd = node.selectionStart;
  75. this.tabcnt+=1;
  76. } else {
  77. node.setValue(this._start + this._currentWord + this._rest);
  78. node.selectionStart = this._caretPos;
  79. node.selectionEnd = this._caretPos;
  80. this.tabcnt = 0
  81. }
  82. if(a.stop) a.stop();
  83. if(a.preventDefault) a.preventDefault();
  84. }
  85. } + ")();";
  86.  
  87. window.setTimeout(function(){
  88. document.body.appendChild(s);
  89. document.body.removeChild(s);
  90. }, 0);