flyink13 / Fast group templates

// ==UserScript==
// @name         Fast group templates
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Подсказки к шаблонам сообщений при вводе (таб - ввыбрать первый)
// @author       Flyink13
// @match        https://vk.com/*
// @copyright 2017, flyink13 (https://openuserjs.org/users/flyink13)
// @license MIT
// ==/UserScript==
/* global hasClass, geByClass1, geByClass, each, val, removeClass, stopEvent, addClass, hide, show, addEvent */
function FastGroupTemplates() {
    if (typeof addEvent != 'function') return;
    if (typeof hasClass != 'function') return;

    var constants = {
    	templatesWrap: 'TemplatesDropDown__wrapper',
    	templatesItem: 'TemplatesDropDown__item',
    	templatesShow: 'TemplatesDropDown__wrapper--show',
    	templatesText: 'TemplatesDropDown__item-content',
    	input: 'im_editable'
    };

    function onKeyUp(event) {
        var el = event.target;
    	if (!hasClass(el, constants.input)) return;

    	var wrap = geByClass1(constants.templatesWrap);
    	var text = val(el).toLocaleLowerCase();
    	var list = geByClass(constants.templatesItem);
    	var item = false;

    	if (!wrap) return;

    	if (event.keyCode == 9 && wrap.activeItem) {
    		var selected = geByClass1(constants.templatesText, wrap.activeItem);

    		val(el, selected.innerHTML.replace(/\n/g, '<br>'));
            removeClass(wrap, constants.templatesShow);
            removeClass(geByClass1('emoji_tt_shown'), 'emoji_tt_shown');
    		stopEvent(event);
    		return false;
    	}

    	addClass(wrap, constants.templatesShow);
    	each(list, function() {
    		var template = this.textContent.toLocaleLowerCase();

    		if (!text || template.indexOf(text) == -1) {
    			return hide(this);
            }

            item = item || this;

            show(this);
    	});

    	wrap.activeItem = item;
    	if (item) return;

        removeClass(wrap, constants.templatesShow);
    	each(list, function() {
    		show(this);
    	});
    }

    addEvent(window, 'keydown', function onKeyDown(event) {
    	var el = event.target;
    	if (!hasClass(el, constants.input) || el.fastTemplate) return;
    	addEvent(el, 'keyup', onKeyUp);
    	el.fastTemplate = true;
    });
}

(function injectScript() {
    var script = document.createElement('script');
    script.appendChild(document.createTextNode('(' + FastGroupTemplates + ')();'));
    (document.body || document.head || document.documentElement).appendChild(script);
})();