行頭に*で見出しを作る
UserScript.icon
https://gyazo.com/b22ca1598e3a47b2ea244261f0695658
空行に * を入力すると [* ] になります
既存の行の行頭に * を入力すると [* ] で囲みます
2024/5/5
使うとページの中身が全て上書きされて[* ]という空ページになってしまう…/kofu-lab/kofu.icon
注意喚起
https://gyazo.com/d2d743a763c5823f789cab6e0f747358
空行に * 入力→Ctrl Zでundoしてる
code:script.js
// 行頭に*で見出しを作る
(() => {
function updateCurrentLine(makeNewText) {
const result = makeNewText(
document.querySelector(".lines div.line.cursor-line").textContent
);
if (!result) {
return;
}
const { text, pos } = result;
const textInput = document.getElementById("text-input");
const keyEvent = (props) =>
new KeyboardEvent("keydown", {
...props,
bubbles: true,
cancelable: true,
});
[
keyEvent({ ctrlKey: true, keyCode: "E".charCodeAt() }),
keyEvent({ ctrlKey: true, shiftKey: true, keyCode: "A".charCodeAt() }),
keyEvent({ keyCode: 8 /* Backspace */ }),
].forEach((ev) => textInput.dispatchEvent(ev));
textInput.value = text;
textInput.focus();
const inputEvent = new InputEvent("input", {
bubbles: true,
cancelable: true,
});
textInput.dispatchEvent(inputEvent);
textInput.dispatchEvent(
keyEvent({ ctrlKey: true, keyCode: "A".charCodeAt() })
);
if (pos) {
for (let i = 0; i < pos; i++) {
textInput.dispatchEvent(keyEvent({ keyCode: 39 /* Right Arrow */ }));
}
}
}
document.addEventListener("keyup", (ev) => {
if (ev.key === "*") {
updateCurrentLine((text) => {
let m;
if (text.substring(0, 3) === "*[*") {
// "*foo" -> "foo"
return { text: text.replace(/^\*\[/, "[*") };
} else if (text === "*") {
return { text: "* ", pos: 3 }; } else if ((m = /^\(\*+) \*\$/.exec(text))) { return { text: [${m[1]}* ], pos: m1.length + 3 }; } else if (text0 === "*") { // "*foo" -> "foo"
return { text: text.replace(/^\*(.+)/, "$1") };
} else {
return null;
}
});
}
});
})();