Last active
May 27, 2023 02:36
-
-
Save yuki2021/3eaac812611d881f8f36f83a56336344 to your computer and use it in GitHub Desktop.
Tampermonkeyにて、Scrapboxで日記ページを作るためのUserScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name scrapbox_add_diary | |
// @version 1.3 | |
// @description Scrapboxにその日の日付の日記ページを追加する。ページが存在する場合は打刻してページを開く。 | |
// @author yuki2021 | |
// @match https://scrapbox.io/* | |
// ==/UserScript== | |
function diffDate(date, diffDays, diffMonths = 0, diffYears = 0) { | |
var d = new Date(date); | |
d.setDate(d.getDate() + diffDays); | |
d.setMonth(d.getMonth() + diffMonths); | |
d.setFullYear(d.getFullYear() + diffYears); | |
var a = [ | |
d.getFullYear(), | |
('00' + (1 + d.getMonth())).slice(-2), | |
('00' + d.getDate()).slice(-2) | |
]; | |
return a.join('/'); | |
} | |
function getCurrentDateString() { | |
var d = new Date(); | |
var year = d.getFullYear(); | |
var dt = d.getDate(); | |
var month = d.getMonth() + 1; | |
var date = [ | |
year, | |
('00' + month).slice(-2), | |
('00' + dt).slice(-2) | |
]; | |
return date.join('/'); | |
} | |
function getPageTags(projectUrl, dateStr) { | |
var days = ["日", "月", "火", "水", "木", "金", "土"]; | |
var d = new Date(dateStr); | |
var year = d.getFullYear(); | |
var month = d.getMonth() + 1; | |
var day = days[d.getDay()]; | |
return [ | |
'[← ' + projectUrl + encodeURIComponent(diffDate(dateStr, -1)) + ']', | |
'#' + year, | |
'#' + month + '月', | |
'#' + day + '曜日', | |
'[1ヶ月前 ' + projectUrl + encodeURIComponent(diffDate(dateStr, 0, -1)) + ']', | |
'[3ヶ月前 ' + projectUrl + encodeURIComponent(diffDate(dateStr, 0, -3)) + ']', | |
'[1年前 ' + projectUrl + encodeURIComponent(diffDate(dateStr, 0, 0, -1)) + ']', | |
'[→ ' + projectUrl + encodeURIComponent(diffDate(dateStr, +1)) + ']' | |
]; | |
} | |
async function pageExists(scrapboxProject, title) { | |
const response = await fetch(`https://scrapbox.io/api/pages/${scrapboxProject}/${encodeURIComponent(title)}`); | |
const data = await response.json(); | |
return data.descriptions && data.descriptions.length > 0; | |
} | |
async function handleKeyPress(event) { | |
if (event.altKey && event.code === 'KeyD') { | |
// 特殊文字入力阻止 | |
event.preventDefault(); | |
const title = getCurrentDateString(); | |
const scrapboxProject = location.href.match(/scrapbox.io\/([^\/.]*)/)[1]; | |
const projectUrl = 'https://scrapbox.io/' + scrapboxProject + '/'; | |
const scrapboxUrl = 'https://scrapbox.io/' + scrapboxProject + '/' + encodeURIComponent(title); | |
if (await pageExists(scrapboxProject, title)) { | |
const now = new Date(); | |
const hour = now.getHours(); | |
const minute = now.getMinutes(); | |
const second = now.getSeconds(); | |
const writeTime = encodeURIComponent('\t' + title + ' ' + hour + ':' + minute + ':' + second); | |
window.open(scrapboxUrl + '?body=' + writeTime); | |
} else { | |
const tags = getPageTags(projectUrl, title); | |
const tagsStr = encodeURIComponent(tags.join(' ')); | |
window.open(scrapboxUrl + '?body=' + tagsStr); | |
} | |
} | |
} | |
document.addEventListener('keydown', handleKeyPress, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment