function initButtons() {
    if ($('div.request-buttons').length === 0) {
        $('<div class="request-buttons"></div>').insertAfter('.askt-simulator__input');
    }
    $('.request-buttons').html('');
    chrome.storage.local.get([skillId], function(result) {
        if (result && result[skillId] && result[skillId].buttons) {
            localeButtons = result[skillId].buttons[locale];
            if (localeButtons) {
                localeButtons.forEach((button) => {
                    addButton(button);
                });
            }
        }
    });

    $('.request-buttons').off();
    $('.request-buttons').on('click','button.post', function() {
        postText($(this).val());
    });
    $('.request-buttons').on('click','button.remove', function() {
        removeButton($(this).val());
    });


    $(".askt-dialog").bind("DOMNodeInserted",function(el){
        if($(el.target).hasClass('askt-dialog__message') &&
            $(el.target).hasClass('askt-dialog__message--request')) {
            $(el.target).prepend(`<span class="asksos-action-icon repost" title="Repost">${redoImage}</span>`);

            // show, if it is not saved already
            if (!localeButtons || !localeButtons.includes($(el.target).parent().text())) {
                $(el.target).prepend(`<span class="asksos-action-icon save" title="Add request button">${saveImage}</span>`);
            }
            //add to history
            addToHistory($(el.target).parent().text());
        }
    });
    // $('.askt-dialog').off();
    $('.askt-dialog').on('click', '.repost', function() {
        const text = $(this).parent().text();
        postText(text);
    });

    $('.askt-dialog').on('click', '.save', function() {
        const text = $(this).parent().text();
        saveButton(text);
    });

    $(".askt-utterance__input").keydown(function(e) {
        chrome.storage.local.get([skillId], function(result) {
            if (result && result[skillId] && result[skillId].history) {
                commandHistory = result[skillId].history[locale];
                if(commandHistory) {
                    switch(e.key) {
                        case "ArrowUp": historyIndex < commandHistory.length-1 ? historyIndex ++ : historyIndex = 0; break;
                        case "ArrowDown": historyIndex > 0 ? historyIndex-- : historyIndex = 0; break;
                        default: return;
                    }
                }

                // Trigger the change event on the input
                var ev = new Event('input', { bubbles: true});
                ev.simulated = true;
                $(".askt-utterance__input")[0].value = commandHistory[historyIndex];
                $(".askt-utterance__input")[0].dispatchEvent(ev);
            }
        });
    });

}
/**
 * Gets buttons from storage and adds to container
 */
Gets buttons from storage and adds to container
Gets buttons from storage and adds to container
function initButtons() {
    if ($('div.request-buttons').length === 0) {
        $('<div class="request-buttons"></div>').insertAfter('.askt-simulator__input');
    }
    $('.request-buttons').html('');
    chrome.storage.local.get([skillId], function(result) {
        if (result && result[skillId] && result[skillId].buttons) {
            localeButtons = result[skillId].buttons[locale];
            if (localeButtons) {
                localeButtons.forEach((button) => {
                    addButton(button);
                });
            }
        }
    });
    $('.request-buttons').off();
    $('.request-buttons').on('click','button.post', function() {
        postText($(this).val());
    });
    $('.request-buttons').on('click','button.remove', function() {
        removeButton($(this).val());
    });
    $(".askt-dialog").bind("DOMNodeInserted",function(el){
        if($(el.target).hasClass('askt-dialog__message') &&
            $(el.target).hasClass('askt-dialog__message--request')) {
            $(el.target).prepend(`<span class="asksos-action-icon repost" title="Repost">${redoImage}</span>`);
            if (!localeButtons || !localeButtons.includes($(el.target).parent().text())) {
                $(el.target).prepend(`<span class="asksos-action-icon save" title="Add request button">${saveImage}</span>`);
            }
            addToHistory($(el.target).parent().text());
        }
    });
    $('.askt-dialog').on('click', '.repost', function() {
        const text = $(this).parent().text();
        postText(text);
    });
    $('.askt-dialog').on('click', '.save', function() {
        const text = $(this).parent().text();
        saveButton(text);
    });
    $(".askt-utterance__input").keydown(function(e) {
        chrome.storage.local.get([skillId], function(result) {
            if (result && result[skillId] && result[skillId].history) {
                commandHistory = result[skillId].history[locale];
                if(commandHistory) {
                    switch(e.key) {
                        case "ArrowUp": historyIndex < commandHistory.length-1 ? historyIndex ++ : historyIndex = 0; break;
                        case "ArrowDown": historyIndex > 0 ? historyIndex-- : historyIndex = 0; break;
                        default: return;
                    }
                }
                var ev = new Event('input', { bubbles: true});
                ev.simulated = true;
                $(".askt-utterance__input")[0].value = commandHistory[historyIndex];
                $(".askt-utterance__input")[0].dispatchEvent(ev);
            }
        });
    });
}
function initButtons ( ) { if ( $ ( 'div.request-buttons' ) . length === 0 ) { $ ( '<div class="request-buttons"></div>' ) . insertAfter ( '.askt-simulator__input' ) ; } $ ( '.request-buttons' ) . html ( '' ) ; chrome . storage . local . get ( [ skillId ] , function ( result ) { if ( result && result [ skillId ] && result [ skillId ] . buttons ) { localeButtons = result [ skillId ] . buttons [ locale ] ; if ( localeButtons ) { localeButtons . forEach ( ( button ) => { addButton ( button ) ; } ) ; } } } ) ; $ ( '.request-buttons' ) . off ( ) ; $ ( '.request-buttons' ) . on ( 'click' , 'button.post' , function ( ) { postText ( $ ( this ) . val ( ) ) ; } ) ; $ ( '.request-buttons' ) . on ( 'click' , 'button.remove' , function ( ) { removeButton ( $ ( this ) . val ( ) ) ; } ) ; $ ( ".askt-dialog" ) . bind ( "DOMNodeInserted" , function ( el ) { if ( $ ( el . target ) . hasClass ( 'askt-dialog__message' ) && $ ( el . target ) . hasClass ( 'askt-dialog__message--request' ) ) { $ ( el . target ) . prepend ( ` ${ redoImage } ` ) ; if ( ! localeButtons || ! localeButtons . includes ( $ ( el . target ) . parent ( ) . text ( ) ) ) { $ ( el . target ) . prepend ( ` ${ saveImage } ` ) ; } addToHistory ( $ ( el . target ) . parent ( ) . text ( ) ) ; } } ) ; $ ( '.askt-dialog' ) . on ( 'click' , '.repost' , function ( ) { const text = $ ( this ) . parent ( ) . text ( ) ; postText ( text ) ; } ) ; $ ( '.askt-dialog' ) . on ( 'click' , '.save' , function ( ) { const text = $ ( this ) . parent ( ) . text ( ) ; saveButton ( text ) ; } ) ; $ ( ".askt-utterance__input" ) . keydown ( function ( e ) { chrome . storage . local . get ( [ skillId ] , function ( result ) { if ( result && result [ skillId ] && result [ skillId ] . history ) { commandHistory = result [ skillId ] . history [ locale ] ; if ( commandHistory ) { switch ( e . key ) { case "ArrowUp" : historyIndex < commandHistory . length - 1 ? historyIndex ++ : historyIndex = 0 ; break ; case "ArrowDown" : historyIndex > 0 ? historyIndex -- : historyIndex = 0 ; break ; default : return ; } } var ev = new Event ( 'input' , { bubbles : true } ) ; ev . simulated = true ; $ ( ".askt-utterance__input" ) [ 0 ] . value = commandHistory [ historyIndex ] ; $ ( ".askt-utterance__input" ) [ 0 ] . dispatchEvent ( ev ) ; } } ) ; } ) ; }
Gets buttons from storage and adds to container
Gets buttons from storage and adds to container
// show, if it is not saved already //add to history // $('.askt-dialog').off(); // Trigger the change event on the input