週記を作るボタンを表示するUserScript
ボタンを押すとhttps://scrapbox.io/projectname/週記 YYYY W[週番号] MM/DD~MM/DDが開かれる
最初のMM/DDにはその週の最初の日、最後のMM/DDにはその週の最後の日が入っている
週は日曜始まりとする
ISO週番号を使用。ただし、日付は日曜始まりにしたいので、たとえば2025-09-14は2025-W37-7だが、次の週と見なし、タイトルは週記 2025 W38 09/14~09/20 のようにする projectnameにはプロジェクト名が入っている
既存のページがあれば既存のページが開かれる✅
既存のページがない場合
新しいページを作成✅
作成時テンプレートが入っているようにする☑️
以下の内容をクリップボードにコピー✅
code:sample
[週記 YYYY W週番号 MM/DD~MM/DD]←週記 YYYY W週番号 MM/DD~MM/DD→[週記 YYYY W週番号 MM/DD~MM/DD] 既存のページがない場合のみクリップボードにコピー☑️
YYYY-MM-DDには
ピン留めを確認し、古い週記がpinされていたらならばpinを外し、新しい周記にpinを付け替える☑️
「週記」から始まるページがピン留めされている場合は、タイトルを確認すればよい?
メモ: 古いコードなどはこっちにあるcak.icon
code:script.js
// 週記作成スクリプト
scrapbox.PageMenu.addMenu({
title: '週記を作成',
onClick: () => {
const now = new Date();
// 日曜日始まりの週の開始日を計算
const dayOfWeek = now.getDay();
const startDate = new Date(now);
startDate.setDate(now.getDate() - dayOfWeek);
const endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + 6);
// ISO週番号を計算(日曜日始まりに調整)
const getWeekNumber = (date) => {
const year = date.getFullYear();
const startOfYear = new Date(year, 0, 1);
const startOfYearDay = startOfYear.getDay();
// 年始から何日経ったか
const dayOfYear = Math.floor((date - startOfYear) / (24 * 60 * 60 * 1000)) + 1;
// 年始の曜日を考慮して週番号を計算(日曜日始まり)
const weekNumber = Math.ceil((dayOfYear + startOfYearDay) / 7);
return weekNumber;
};
const formatDateShort = (date) => {
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return ${month}/${day};
};
const year = startDate.getFullYear();
const weekNumber = getWeekNumber(startDate);
const startDateStr = formatDateShort(startDate);
const endDateStr = formatDateShort(endDate);
const pageTitle = 週記 ${year} W${weekNumber} ${startDateStr}~${endDateStr};
// 年間進捗率を計算
const calculateYearProgress = (date) => {
const year = date.getFullYear();
const startOfYear = new Date(year, 0, 1);
const endOfYear = new Date(year + 1, 0, 1);
const dayOfYear = Math.floor((date - startOfYear) / (24 * 60 * 60 * 1000)) + 1;
const totalDaysInYear = Math.floor((endOfYear - startOfYear) / (24 * 60 * 60 * 1000));
return Math.round((dayOfYear / totalDaysInYear) * 100);
};
// テンプレート生成
const generateTemplate = () => {
const progress = calculateYearProgress(now);
const formatDateFull = (date) => {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return ${year}-${month}-${day};
};
// 各曜日の日付を生成(土曜日から日曜日の順)
const dayEntries = [];
for (let i = 6; i >= 0; i--) {
const dayDate = new Date(startDate);
dayDate.setDate(startDate.getDate() + i);
const dateStr = formatDateFull(dayDate);
const dayName = weekDaysi; dayEntries.push( [${dateStr}] ${dayName});
}
// 前後の週記タイトルを生成
const prevWeekStart = new Date(startDate);
prevWeekStart.setDate(startDate.getDate() - 7);
const prevWeekEnd = new Date(endDate);
prevWeekEnd.setDate(endDate.getDate() - 7);
const prevWeekNumber = getWeekNumber(prevWeekStart);
const prevWeekTitle = 週記 ${prevWeekStart.getFullYear()} W${prevWeekNumber} ${formatDateShort(prevWeekStart)}~${formatDateShort(prevWeekEnd)};
const nextWeekStart = new Date(startDate);
nextWeekStart.setDate(startDate.getDate() + 7);
const nextWeekEnd = new Date(endDate);
nextWeekEnd.setDate(endDate.getDate() + 7);
const nextWeekNumber = getWeekNumber(nextWeekStart);
const nextWeekTitle = 週記 ${nextWeekStart.getFullYear()} W${nextWeekNumber} ${formatDateShort(nextWeekStart)}~${formatDateShort(nextWeekEnd)};
${dayEntries.join('\n')}
};
const template = generateTemplate();
// クリップボードにコピー
navigator.clipboard.writeText(template).then(() => {
console.log('テンプレートをクリップボードにコピーしました');
}).catch(err => {
console.error('クリップボードへのコピーに失敗しました:', err);
// フォールバック: テキストエリアを使用
const textArea = document.createElement('textarea');
textArea.value = template;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
console.log('フォールバック方式でテンプレートをコピーしました');
});
const projectName = scrapbox.Project.name;
const pageUrl = https://scrapbox.io/${projectName}/${encodeURIComponent(pageTitle)};
window.open(pageUrl, '_self');
console.log('週記ページを開きました:', pageTitle);
console.log('生成されたテンプレート:\n', template);
},
});