Username-completion

Tab cycles through users starting with characters

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name           Username-completion
// @namespace      tag://kongregate
// @description    Tab cycles through users starting with characters
// @include        http://www.kongregate.com/games/*
// @author         Ventero
// @date           03.01.2012
// @version        1.5
// require        http://kong.ventero.de/updates/49872.js
// ==/UserScript==

// Written by Ventero (http://www.kongregate.com/accounts/Ventero) 05/23/09
// Thanks to kaedenn for the idea of adding a colon if the username is the first word in the message
// Copyright (c) 2009-2012 Ventero, licensed under MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php

var s = document.createElement("script");
s.textContent = "(" +
function init_nameCompletion(){
	if(typeof ChatDialogue === "undefined" ||
	   ChatDialogue.prototype.oldKeyPressTab) return;

	var isChrome = (navigator.appVersion.indexOf("Chrome") >= 0);
	if(isChrome) {
		ChatDialogue.prototype.initialize =
		ChatDialogue.prototype.initialize.wrap(function(old, p, i, h, u){
			old(p, i, h, u);
			var self = this;
			this._input_node.observe("keydown", function(event) {
				if(event.keyCode != 9 || event.ctrlKey || event.altKey || event.metaKey) return;
				self.onKeyPress(event);
			});
		})
	}

	ChatDialogue.prototype.oldKeyPressTab = ChatDialogue.prototype.onKeyPress;
	ChatDialogue.prototype.tabcnt = 0;
	ChatDialogue.prototype.done = 1;
	ChatDialogue.prototype.onKeyPress = function(a){
		if (a.keyCode != 9 || a.ctrlKey){
			this.tabcnt = 0;
			this.done = 1;
			this.oldKeyPressTab(a);
			return;
		}

		var node = (this._input_node.wrappedJSObject || this._input_node);
		if (this.tabcnt == 0 && this.done == 1){
			var inputText = node.getValue(),
			    spaceAtCaret = inputText.substr(0, node.selectionStart).lastIndexOf(' ');
			this._caretPos = node.selectionStart;
			this._start = inputText.substr(0,spaceAtCaret);
			if(this._start) this._start+=" ";

			this._currentWord = inputText.substring(spaceAtCaret+1, this._caretPos);
			this._rest = inputText.substr(this._caretPos);
		}
		this.done = 0;

		var userArray = this._holodeck.chatWindow().activeRoom()._users_list,
		    possibleMatches = [],
		    z = node.getValue();
		if (z.match(/\s+$/)) z=z.replace(/\s+$/, '')

		for (var i=0;i<userArray.length;i++){
			if(userArray[i].username.toLowerCase().indexOf(this._currentWord.toLowerCase())==0){
				possibleMatches.push(userArray[i].username);
			}
		}

		if (this.tabcnt < possibleMatches.length){
			node.setValue(this._start + possibleMatches[this.tabcnt] + (this._start?" ":": ") + this._rest);
			node.selectionStart = this._caretPos + possibleMatches[this.tabcnt].length - this._currentWord.length+(this._start?1:2);
			node.selectionEnd = node.selectionStart;
			this.tabcnt+=1;
		} else {
			node.setValue(this._start + this._currentWord + this._rest);
			node.selectionStart = this._caretPos;
			node.selectionEnd = this._caretPos;
			this.tabcnt = 0
		}
		if(a.stop) a.stop();
		if(a.preventDefault) a.preventDefault();
	}
} + ")();";

window.setTimeout(function(){
	document.body.appendChild(s);
	document.body.removeChild(s);
}, 0);