otofune
↑ SendGrid みたいだね
こいつは新規投稿を押したときに記事テンプレを適用してくれる UserScript
script.js(() => {
const fill = (s, length, fill) =>
`${fill}`.repeat(length - `${s}`.length) + s;
const project = "otofune-diary";
const sleep = (ms) => new Promise((res) => setTimeout(() => res(), ms));
const isPageCreated = async (title) => {
const page = await fetch(`/api/pages/${project}/${title}`).then((r) =>
r.json()
);
if (page.descriptions.length) {
console.log(`${title} was already created!`);
return true;
}
return false;
};
const onNew = async () => {
const now = new Date();
const toString = (d) =>
`${d.getFullYear()}.${fill(d.getMonth() + 1, 2, 0)}.${fill(
d.getDate(),
2,
0
)}`;
const newPageTitle = toString(now);
if (await isPageCreated(newPageTitle)) {
console.log("already created!");
return;
}
const prev = new Date();
prev.setDate(now.getDate() - 1);
const next = new Date();
next.setDate(now.getDate() + 1);
const body = `<- [${toString(next)}] / [${toString(
prev
)}] ->\n#日記 #${now.getFullYear()}.${now.getMonth() + 1}`;
// ?body= の URL が履歴に残ったりすると追記が走って不愉快なので、iframe で作成させてから遷移する
const createIframe = document.createElement("iframe");
createIframe.style =
"position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 10000;";
createIframe.src = `/${project}/${newPageTitle}?body=${encodeURIComponent(
body
)}`;
document.body.appendChild(createIframe);
while (true) {
console.log("sleeping while document created");
if (await isPageCreated(newPageTitle)) break;
await sleep(200);
}
location.href = `/${project}/${newPageTitle}`;
};
const onMove = (p) => {
console.log(p);
if (p.endsWith(`/${project}/new`)) {
console.log("hooking new");
onNew(p);
}
};
const enableEventHandleOnPushState = (h) => () => {
const orig = history.pushState.bind(history);
history.pushState = (state, title, path) => {
h(path);
return orig(state, title, path);
};
};
enableEventHandleOnPushState(onMove)();
})();
script.jsimport 'https://scrapbox.io/api/code/ci7lus/tweet-card/script.js';
script.jsimport 'https://scrapbox.io/api/code/rinsuki/nicothumb2img/nicothumb2img.js';