const textInput = document.getElementById('text-input'); export function execute() { document.addEventListener('compositionupdate', e => { const buttons = document.getElementsByClassName('popup-menu') ?.[0]?.getElementsByClassName('button') if (!buttons) return; // そもそもpopup menuがなかったら何もしない if (e.data.length === 0) return; switch(e.data.split('').pop()) { case '「': e.preventDefault(); e.stopPropagation(); document.getElementsByClassName('button link-button')?.[0]?.click(); textInput.blur();// IMEの文字列を強制的に確定させる deleteBackward(e.data.length);// 確定して流し込まれた文字列を削除する textInput.focus();// カーソルのfocusを戻す break; default: break; } }); } function deleteBackward(count) { const deleteParam = {keyCode: 8, key: 'Backspace', bubbles: true, cancelable: true}; for (let i = 0; i < count; i++) { textInput.dispatchEvent(new KeyboardEvent('keydown', deleteParam)); textInput.dispatchEvent(new KeyboardEvent('keyup', deleteParam)); } } execute()