Scrapboxのページの差分書き換えテスト
scrapbox-headless-scriptのpatch()が実際に動くかどうか確かめる
2021-10-28
20:58:57 タイトルを書き換えるとバグる
タイトル対応は後日考える
21:04:57 複数行でバグってる
21:44:19 O(NP) algorithmを変換するflush()のアルゴリズムがおかしかった
全ての追加と削除のペアを変更に変換しきれていない
22:13:20 修正完了
22:20:13 いい感じ
適当に書いたメモ
書き込みますよ
あれ?、書き込まれない?
なるほど。空ページのIDはアクセスするたびに変わるから、空ページにアクセスした状態でこのscriptを起動しても、そのページの空状態が変わるわけではないのか。takker.icon
書き込みますよ
真ん中にも書き込める
リンクも書き込める
takker
ちゃんと書き込まれている
複数行
を一気に書き込むことも
もちろんできる
全消しもできるぞtakker.icon*5
後日対応すること
タイトルの書き換え
descriptionの更新
linksの更新
これは難しいかも
bundled code
Scrapboxのページの差分書き換えテスト (code)
元のコード
UIはScrapbox UserScriptからSocket.IOを使を流用した
code:app.ts
/// <reference no-default-lib="true" />
/// <reference lib="esnext" />
/// <reference lib="dom" />
import { joinPageRoom } from "https://pax.deno.dev/takker99/scrapbox-headless-script@0.1.0";
import { throttle } from "https://pax.deno.dev/takker99/deno-async-throttle@v0.1.0";
import { Scrapbox } from "https://pax.deno.dev/scrapbox-jp/types@0.0.5";
declare const scrapbox: Scrapbox;
const project = "takker";
const title = "差分書き込みテスト用ページ";
const div = document.createElement("div");
const shadowRoot = div.attachShadow({ mode: "open" });
shadowRoot.innerHTML = `
<style>
:host {
position: fixed;
left: 0;
bottom: 0;
z-index: 9999;
text-decoration: none;
background-color: var(--page-bg);
color: var(--page-text-color);
border: solid 1px var(--telomere-unread);
border-radius: 4px;
padding: 8px;
margin: 10px;
}
button {
display: inline-block;
}
</style>
<div>
<button id="button">x</button>
<span id="title"></span>
</div>
<textarea id="editor"></textarea>
`;
document.body.append(div);
const { patch, cleanup } = await joinPageRoom(project, title);
console.info("Connected to scrapbox.io");
const editor = shadowRoot.getElementById("editor")! as HTMLTextAreaElement;
const update = throttle((text: string) =>
patch((titleLine) => titleLine.text, ...text.split(/\n|\r\n/)),
{ trailing: true }
);
editor.addEventListener("input", () => update(editor.value));
console.info("Start patching");
shadowRoot.getElementById("title")!.insertAdjacentHTML(
"beforeend",
<a href="${/${project}/${title}}" target="_blank">${/${project}/${title}}</a>,
);
shadowRoot.getElementById("button")!.onclick = () => {
console.info("End patching");
cleanup();
div.remove();
};
#2021-11-07 10:23:02 refactoring
#2021-10-28 20:30:43