Scrapbox上の操作の再実行

Scrapbox上の操作の再実行

Scrapboxのキー操作を再実行する
超簡単キーボードマクロみたいなかんじ

Scrapboxの自分のペ ージに以下の記述をする
code:script.js

Ctrl-R で、それまでのキー操作を再実行できるようになる
それまでの操作とは以下のもの
2秒以上キー操作を行なわなかったときから現在までのキー操作
なんらかのマウス操作をしてから現在までのキー操作
イマイチ直感的でないので他の方法も考えたい
慣れの問題なのか? そうではないのか?


script.js
Copied!
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 == 'r') { // Ctrl-R で 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) => { 初期化() })
Powered by Helpfeel