長いタイトルを刻むページを簡単に作れるUserScript
複数行では実行しない
たくさん作るとこうなる
2023-04-30
バグの原因がわからない
2023-02-06
このページのUserScriptはdeprecatedとする
2023-02-04
05:30:34 わざわざ作る必要あった?
エッジケースすぎる
$ deno check --remote -r=https://scrapbox.io https://scrapbox.io/api/code/takker/長いリンクを刻むページを簡単に作れるUserScript/main.ts
main.tsimport { useStatusBar, patch } from "../scrapbox-userscript-std/mod.ts";
import { delay as sleep } from "../deno_std%2Fasync/mod.ts";
import { Scrapbox } from "../scrapbox-jp%2Ftypes/userscript.ts";
declare const scrapbox: Scrapbox;
const enable = (text: string) =>
!text.includes("\n") && /\[[^\]]+\]/.test(text);
scrapbox.PopupMenu.addButton({
title: (text) => enable(text) ? "[][]" : "",
onClick: (text) => {
if (!enable(text)) return;
const title = text.replaceAll("[", "").replaceAll("]", "");
(async () => {
const { render, dispose } = useStatusBar();
try {
const project = scrapbox.Project.name;
render(
{ type: "spinner" },
{ type: "text", text: `create /${project}/${title}` },
);
await patch(
project,
title,
([{ text: title }], { persistent }) => {
// 中身があるときは何もしない
if (persistent) return;
return [title, text];
},
);
render(
{ type: "check-circle" },
{ type: "text", text: `create /${project}/${title}` },
);
} catch(e) {
console.error(e);
if (!(e instanceof Error)) throw e;
render(
{ type: "exclamation-triangle" },
{ type: "text", text: `${e.name} ${e.message}` },
);
} finally {
await sleep(2000);
dispose();
}
})();
return `[${title}]`;
},
});