generated at
キー操作を繰り返すUserScript
キー操作を再実行するUserScript
/masui/Dynamic MacroのScrapbox版?あんも

便利そうなので使ってみたいあんも
簡単なテキスト整形に使えそう
2023をベースに2024をつくるのに使ってみたあんも
IMEがオンになっていると意図したとおりに動かない


/again/Scrapboxでは Ctrl-R が実行キーだが、ページリロードに食われてしまうので Ctrl-Space にしたあんも
JavaScriptでも変数名に日本語が使えるのか
script.js
const 修飾キー = ['Control', 'Alt', 'Shift', 'Meta', 'Unidentified'] const 特殊キー = ['Enter', 'Backspace', 'Delete', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'] let キー操作列 = []; // 繰り返し実行されるキー操作列 let マクロ実行中 = false; let 無操作タイムアウト = null const 制御キー実行 = (キー) => { let { keyCode, ctrlKey, altKey, shiftKey, metaKey } = JSON.parse(キー) let e = document.createEvent('Events') e.initEvent('keydown', true, true) e.keyCode = e.which = keyCode e.ctrlKey = ctrlKey e.altKey = altKey e.shiftKey = shiftKey e.metaKey = metaKey $('#text-input')[0].dispatchEvent(e) } const 文字列挿入 = (キー) => { document.execCommand('insertText', null, キー) } const 初期化 = () => { キー操作列 = [] マクロ実行中 = false } const 無操作タイムアウト更新 = () => { clearTimeout(無操作タイムアウト) 無操作タイムアウト = setTimeout(初期化,2000) } $('#text-input').on('keydown', (e) => { if (e.ctrlKey && e.key == ' ') { // Ctrl-Space で again() 実行 無操作タイムアウト更新() マクロ実行中 = true // キー操作列[] のキーを実行 for (let キー of キー操作列){ if (キー.match(/^{.*}$/)) { 制御キー実行(キー) } else { // InputEvent 文字列挿入(キー) } } return } if (マクロ実行中) return; 無操作タイムアウト更新() if (!e.key || 修飾キー.includes(e.key)) return if (!特殊キー.includes(e.key) && !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) return キー操作列.push(JSON.stringify({ keyCode: e.keyCode, ctrlKey: e.ctrlKey, altKey: e.altKey,shiftKey: e.shiftKey, metaKey: e.metaKey })) }) $('#text-input').on('input', ({ originalEvent }) => { 無操作タイムアウト更新() if (originalEvent.data != 'getIndexByTitleLc' && !マクロ実行中) { キー操作列.push(originalEvent.data) } }); // マウス操作があれば初期化する $('body').on('click',(e) => { 初期化() }) $('body').on('mousemove',(e) => { 初期化() })