Skip to content

Instantly share code, notes, and snippets.

@xbenjii
Last active March 4, 2016 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xbenjii/85bbb5047ec064e385bb to your computer and use it in GitHub Desktop.
Save xbenjii/85bbb5047ec064e385bb to your computer and use it in GitHub Desktop.
Tab Reply
const input = $('#input');
const yourName = $('#active-user .avatar img').attr('title').replace(/\s/g,"");
const mention = `@${yourName}`;
$(document).on('keydown', (e) => {
if(e.which === 9) {
if($('#autocomplete').children().length) {
return false;
}
e.preventDefault();
const messages = $('.message');
$(messages.get().reverse()).each((i, message) => {
const $message = $(message);
if($message.find('.mention').text() === mention) {
$message.find('.newreply').click();
return false;
}
});
}
});
@rlemon
Copy link

rlemon commented Mar 4, 2016

"use strict";
const input = document.getElementById('input');
const chat = document.getElementById('chat');
const myid = document.getElementById('active-user').className.match(/user-(\d+)/)[1];

input.addEventListener('keydown', processKeydown);

function processKeydown(event) {
    if( event.which === 9 && input.value.length === 0 ) {
        event.preventDefault();
        const last = getLastReply();
        console.log(last);
        input.value  = ':' + last + ' '; // template strings don't work in chrome stable yet :/
    }
}

function getLastReply() {
    const mentions = chat.querySelectorAll('.mention');
    const lastMention = Array.from(mentions).pop();
    return lastMention.parentNode.parentNode.id.split('-')[1];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment