Roll20 Quick Character Selection

Pressing the graves key (`) in the chatbox will query the character list for a Roll20 game and select the first match. Easy.

Version vom 17.07.2018. Aktuellste Version

// ==UserScript==
// @name         Roll20 Quick Character Selection
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Pressing the graves key (`) in the chatbox will query the character list for a Roll20 game and select the first match. Easy.
// @author       Ephemeralis
// @match        https://app.roll20.net/editor/
// @grant        none
// ==/UserScript==

console.log("Roll20 Quick Char Selection loaded!");
window.G20_ClearChat = false;

function searchForCharacter(qn)
{
    var changed = false;
        $('#speakingas > option').each(function () {
            if (this.text.toLowerCase().indexOf(qn.toLowerCase()) !== -1)
            {
                //found a match, so set the speaking as value and we're good
                //$('#textchat-input > textarea').text = "test"
                $('#speakingas').val(this.value).change();
                //then clear the textbox of the search value afterwards
                //$('#textchat-input > textarea').text("").change();
                changed = true;
                return false;
            }
        } );
   return changed;
}

window.searchForCharacter = searchForCharacter;

function doc_keyDown(e)
{
    switch (e.keyCode)
    {
        case 192: //graves `
            if (document.activeElement.parentNode.id == "textchat-input")
            {
                var search = $('#textchat-input > textarea').val();
                console.log(search);
                if (searchForCharacter(search))
                {
                    console.log("search term matched! clearing textbox on keyup...");
                    window.G20_ClearChat = true;
                }
            }
            break;
    }
}

function doc_keyUp(e)
{
    switch (e.keyCode)
    {
        case 192:
            if (document.activeElement.parentNode.id == "textchat-input" && window.G20_ClearChat == true)
            {
                //reset the chatbox text on good match
                console.log("Resetting R20 chat text area...");
                $('#textchat-input > textarea').val("").change();
                window.G20_ClearChat = false;
            }
          break;
    }
}


document.addEventListener('keydown', doc_keyDown, false);
document.addEventListener('keyup', doc_keyUp, false);