Skip to content

Instantly share code, notes, and snippets.

@yuki2021
Last active May 27, 2023 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuki2021/656401aba3ea9726e9628c64fb8c5c93 to your computer and use it in GitHub Desktop.
Save yuki2021/656401aba3ea9726e9628c64fb8c5c93 to your computer and use it in GitHub Desktop.
Tampermonkeyにて、Scrapboxで週報を作るためのUserScript
// ==UserScript==
// @name scrapbox_week_report
// @version 1.1
// @description Scrapboxに週報のページを追加する
// @author yuki2021
// @match https://scrapbox.io/*
// ==/UserScript==
function weekOfMonth(date) {
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1).getDay();
return Math.ceil((date.getDate() + firstDay) / 7);
};
function diffDate(date, diff) {
var d = new Date(date);
d.setDate(d.getDate() + diff);
var a = [
d.getFullYear(),
('00' + (1 + d.getMonth())).slice(-2),
('00' + d.getDate()).slice(-2)
];
return a.join('/');
};
function diffWeek(date, diff) {
var d = new Date(date);
d.setDate(d.getDate() + (diff * 7));
var year = d.getFullYear();
var month = d.getMonth() + 1;
var week = weekOfMonth(d);
return year + "年" + month + "月第" + week + "週の振り返り";
};
function getWeekDays(date) {
var days = ["日", "月", "火", "水", "木", "金", "土"];
var weekDays = [];
for(var i = 6; i >= 0; i--) {
var d = new Date(date);
d.setDate(d.getDate() - i - 1); // 入力日の前日から開始
weekDays.push('\t[' + diffDate(d, 0) + ']' + days[d.getDay()] + '曜日\n\t\t');
}
return weekDays.join('\n');
};
function handleKeyPress(event) {
// macのキーマップでOption + Wが押された時に発動
if (event.altKey && event.code === 'KeyW') {
// 特殊文字入力を防ぐ
event.preventDefault();
var inputDate = window.prompt("日付を入力してください (例: yyyy/mm/dd)", "");
if(inputDate) {
var inputDateObj = new Date(inputDate);
if(!isNaN(inputDateObj)) {
var year = inputDateObj.getFullYear();
var month = inputDateObj.getMonth() + 1;
var week = weekOfMonth(inputDateObj);
var title = diffWeek(inputDateObj, -1);
var scrapboxProject = location.href.match(/scrapbox.io\/([^\/.]*)/)[1];
var projectUrl = 'https://scrapbox.io/' + scrapboxProject + '/';
var tags = [
'[' + diffWeek(inputDateObj, -2) +']',
'#' + year,
'#' + month + '月',
'#第' + week + '週',
'[' + diffWeek(inputDateObj, 0) +']'
];
var body = [
getWeekDays(inputDateObj),
'\t雑感\n\t\t',
'\t先週の目標の振り返り\n\t\t',
'\tKeep(このまま継続し続けること)(よかったこと)\n\t\t',
'\tProblem(これまでの問題点・これからの改善点)(悪かったこと)\n\t\t',
'\tTry(これから挑戦すること)\n\t\t',
'\t来週の目標\n\t\t',
'\n#一週間の振り返りリスト\n#週間振り返り',
].join('\n');
body = encodeURIComponent(tags.join(' ') + '\n' + body);
var scrapboxUrl = 'https://scrapbox.io/' + scrapboxProject + '/' + encodeURIComponent(title);
window.open(scrapboxUrl + '?body=' + body);
} else {
alert("正しい日付を入力してください");
}
}
}
}
document.addEventListener('keydown', handleKeyPress, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment