Skip to content

Instantly share code, notes, and snippets.

@xbenjii xbenjii/tabReply.js
Last active Mar 4, 2016

Embed
What would you like to do?
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

This comment has been minimized.

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
You can’t perform that action at this time.