script.js// Cosense userscript: /api/code/nishio/MyUserScript/script.js などに追記
const project = "nishio";
const titlePrefix = "日記";
function ymd(d = new Date()) {
const y = d.getFullYear();
const m = String(d.getMonth() + 1).padStart(2, "0");
const day = String(d.getDate()).padStart(2, "0");
return `${y}-${m}-${day}`;
}
function toTitle(d) {
return `${titlePrefix}${ymd(d)}`; // "日記yyyy-MM-dd"
}
// JSTはDSTなしなので日単位加減算はこれでOK
function addDays(date, n) {
const d = new Date(date.getFullYear(), date.getMonth(), date.getDate());
d.setDate(d.getDate() + n);
return d;
}
function subDays(date, n) {
return addDays(date, -n);
}
// date-fns subYears 的に 2/29 を 2/28 に丸める
function subYearsSafe(date, n) {
const d = new Date(date.getFullYear(), date.getMonth(), date.getDate());
const origMonth = d.getMonth();
d.setFullYear(d.getFullYear() - n);
if (d.getMonth() !== origMonth) d.setDate(0); // 前月末に丸め
return d;
}
function footerLines(today) {
const yesterday = toTitle(subDays(today, 1));
const t = toTitle(today);
const tomorrow = toTitle(addDays(today, 1));
const d100 = toTitle(subDays(today, 100));
const y1 = toTitle(subYearsSafe(today, 1));
// pin-diary-X と同じ表記: [昨日]←今日→[明日] / 100日前 [..] / 1年前 [..]
return [
`[${yesterday}]←${t}→[${tomorrow}]`,
`100日前 [${d100}]`,
`1年前 [${y1}]`,
];
}
scrapbox.PageMenu.addItem({
title: "日記作成",
onClick: () => {
const today = new Date();
const title = toTitle(today);
// 本文は空で、末尾にテンプレフッターだけ入れる(上に自由に書ける)
const body = encodeURIComponent(["", "", ...footerLines(today)].join("\n"));
window.open(
`https://scrapbox.io/${project}/${encodeURIComponent(title)}?body=${body}`,
"_blank",
);
},
});