今日の日付にリンクしたページの一覧を取得
こんな感じ:
https://gyazo.com/5c437dd8addab56d77c4d50677a86848
日付フォーマットを指定することができます
code:script.js
const USERS_FORMAT_STRING = ''; // 使いたいフォーマットがある方はこちらに入力してください。h, n, sには非対応です(e.g. 'YYYY-MM-DD')
class DateFormatter {
constructor (formatString) {
this.date = new Date()
this.formatString = formatString
if (!formatString) {
this.formatString = 'YYYY/M/D'
}
}
format () {
const ZERO_PADDING = (str, length) => ('0'.repeat(length) + str).slice(length * -1)
const MATCH_YEAR = /Y{1,4}/g
const MATCH_MONTH = /M{1,4}/g
const MATCH_DAY = /D{1,4}/g
const REPLACE_YEAR = match => {
const year = this.date.getFullYear()
switch (match.length) {
case 1:
case 2:
return ZERO_PADDING(year, 2)
case 3:
case 4:
return year
}
}
const REPLACE_MONTH = match => {
const month = this.date.getMonth()
switch (match.length) {
case 1:
return month + 1
case 2:
return ZERO_PADDING(month + 1, 2)
case 3:
return ['Jan' , 'Feb', 'Mar' , 'Apr', 'May', 'June',
'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']month case 4:
return ['January' , 'February', 'March' , 'April' ,
'May' , 'June' , 'July' , 'August',
'September', 'October' , 'November', 'December']month }
}
const REPLACE_DAY = match => {
const day = this.date.getDate()
switch (match.length) {
case 1:
return day
case 2:
return ZERO_PADDING(day, 2)
case 3:
case 4:
return ZERO_PADDING(day, match.length - 1)
}
}
let result = this.formatString
result = result.replace(MATCH_YEAR , REPLACE_YEAR )
result = result.replace(MATCH_MONTH, REPLACE_MONTH)
result = result.replace(MATCH_DAY , REPLACE_DAY )
return result
}
}
class menuBuilder {
constructor (title, image) {
this.Title = title
this.Image = image
scrapbox.PageMenu.addMenu({
title : this.Title,
image : this.Image,
onClick: () => {
this.DateFormatter = new DateFormatter(USERS_FORMAT_STRING)
this.set1hopLinks()
}
})
}
set1hopLinks () {
const NEED_ESCAPE_SYMBOLS = /?\/#/g const DATE = this.DateFormatter.format()
const DATE_ESCAPED = DATE.replace(NEED_ESCAPE_SYMBOLS, match => encodeURIComponent(match))
const PROJECT_NAME = scrapbox.Project.name
const DATE_PAGE_URL = /api/pages/${PROJECT_NAME}/${DATE_ESCAPED}
const ASSIGN_MENU = pagelist => {
const LINKS_1HOP = pagelist.relatedPages.links1hop
scrapbox.PageMenu(this.Title).addItem({
title : DATE,
onClick: () => {}
})
if (LINKS_1HOP.length == 0) {
scrapbox.PageMenu(this.Title).addItem({
title : 'No Link',
onClick: () => {}
})
return
}
for (let link of LINKS_1HOP) {
const LINK_TITLE = link.title
const LINK_URL = /${PROJECT_NAME}/${LINK_TITLE}
scrapbox.PageMenu(this.Title).addItem({
title : LINK_TITLE,
onClick: () => location.href = LINK_URL
})
}
}
scrapbox.PageMenu(this.Title).removeAllItems()
fetch(DATE_PAGE_URL, {
method: "GET",
})
.then(Response => Response.json())
.then(pagelist => ASSIGN_MENU(pagelist))
}
}
UserScript.icon