日付一覧を自動生成するUserScript
TODO:なんか動かなくなったので修正が必要
https://gyazo.com/85476957b862f0dc6442a9268cbc2884
https://gyazo.com/d9efc4cb315b7ce267d1ec3ca12f79ea
https://gyazo.com/84fc1310a6127ac7bb122ea990eb1a68
Insert Today
今日の日付が生成されます
Insert ThisWeek
今週の日付一覧が生成されます
Insert ThisMonth
今月の日付一覧が生成されます
Insert ThisYear
今年1年間の日付一覧が生成されます
Insert AnyWeek
任意の日付を指定するとその日付を含む月曜~日曜の1週間分の日付が生成されます
Insert AnyMonth
任意の年と月を指定するとその月の日付一覧が生成されます
Insert AnyYear
任意の年を指定するとその年の日付一覧が生成されます
hr.icon
実装
code:script.js
const DEBUG_MENU_TITLE = 'Calender';
class CalenderBuilder {
constructor () {
this.date = new Date()
}
today (some_date) {
this.date = new Date(some_date)
const today_y = this.date.getFullYear();
const today_m = this.date.getMonth()+1;
const today_d = this.date.getDate();
}
week (some_date) {
this.date = new Date(some_date);
this.date.setDate(this.date.getDate()-this.date.getDay()+1);
const oneJan = new Date(this.date.getFullYear(), 0, 1);
const numberOfDays = Math.floor((this.date - oneJan) / (24 * 60 * 60 * 1000));
const weekNum = Math.ceil((this.date.getDay() + 1 + numberOfDays) / 7);
const week_str = [];
week_str.push(this.date.getFullYear()+"-w"+weekNum);
for (let i=0; i < 7; i++) {
week_str.push(this.date.getFullYear()+"-"+('0'+(this.date.getMonth()+1)).slice(-2)+"-"+('0'+this.date.getDate()).slice(-2))+"]";
this.date.setDate(this.date.getDate()+1);
}
const text = `
Day Date
`;
return (text);
}
month (some_date) {
this.date = new Date(some_date);
const first_date = new Date(this.date.getFullYear(), this.date.getMonth(), 1);
const end_date = new Date(first_date.getFullYear(), first_date.getMonth()+1, 0);
const end_month = end_date.getDate();
const month_arr = []
let text = `
table:${first_date.getFullYear()}-${('0'+(first_date.getMonth()+1)).slice(-2)}
const space_count = (first_date.getDay() == 0) ? 7 : first_date.getDay();
for (let i=0; i < space_count; i++) {
text += ('\t');
}
const tmp_date = new Date(first_date);
for (let i=0; i < end_month; i++) {
const y_str = tmp_date.getFullYear();
const m_str = ('0'+(tmp_date.getMonth()+1)).slice(-2);
const d_str = ('0'+tmp_date.getDate()).slice(-2);
text += '+ y_str + '-' + m_str + '-' + d_str + '';
if (tmp_date.getDate() === end_month) {
text += (tmp_date.getDay() === 0) ? "" : "\t".repeat(7-end_date.getDay());
} else {
text += (tmp_date.getDay() % 7 === 0) ? "\n\t" : "\t";
}
tmp_date.setDate(tmp_date.getDate()+1);
}
const prev_month = new Date(first_date);
prev_month.setMonth(first_date.getMonth()-1);
const next_month = new Date(first_date);
next_month.setMonth(first_date.getMonth()+1);
text += `
`;
return (text);
}
year (some_date) {
this.date = new Date(some_date);
let text = '';
for (let i=0; i < 12; i++) {
text += this.month(new Date(this.date.getFullYear(), i, 1));
}
return (text);
}
}
// ----------
// メニュー
// ----------
class MenuBuilder {
constructor (title, image) {
this.Title = title;
this.Image = image;
scrapbox.PageMenu.addMenu({
title: this.Title,
image: this.Image,
onClick: () => {
this.CalenderBuilder = new CalenderBuilder();
scrapbox.PageMenu(DEBUG_MENU_TITLE).removeAllItems();
scrapbox.PageMenu(DEBUG_MENU_TITLE).addItem({
title: 'Insert Today',
onClick: () => {
document.execCommand('insertText', false, this.CalenderBuilder.today(new Date()));
}
});
scrapbox.PageMenu(DEBUG_MENU_TITLE).addItem({
title: 'Insert ThisWeek',
onClick: () => {
document.execCommand('insertText', false, this.CalenderBuilder.week(new Date()));
}
});
scrapbox.PageMenu(DEBUG_MENU_TITLE).addItem({
title: 'Insert ThisMonth',
onClick: () => {
document.execCommand('insertText', false, this.CalenderBuilder.month(new Date()));
}
});
scrapbox.PageMenu(DEBUG_MENU_TITLE).addItem({
title: 'Insert ThisYear',
onClick: () => {
document.execCommand('insertText', false, this.CalenderBuilder.year(new Date()));
}
});
scrapbox.PageMenu(DEBUG_MENU_TITLE).addItem({
title: 'Insert AnyWeek',
onClick: () => {
const tmp_date = window.prompt('Please input any YYYY-MM-DD');
document.execCommand('insertText', false, this.CalenderBuilder.week(tmp_date.split('-')));
}
});
scrapbox.PageMenu(DEBUG_MENU_TITLE).addItem({
title: 'Insert AnyMonth',
onClick: () => {
const tmp_date = window.prompt('Please input any YYYY-MM');
document.execCommand('insertText', false, this.CalenderBuilder.month(tmp_date.split('-')));
}
});
scrapbox.PageMenu(DEBUG_MENU_TITLE).addItem({
title: 'Insert AnyYear',
onClick: () => {
const tmp_date = window.prompt('Please input any YYYY');
document.execCommand('insertText', false, this.CalenderBuilder.year(tmp_date));
}
});
scrapbox.PageMenu(DEBUG_MENU_TITLE).addItem({
title: 'Setting',
onClick: () => location.href = '日付一覧を自動生成するUserScript'
});
}
})
}
}
(() => new MenuBuilder(DEBUG_MENU_TITLE, DEBUG_MENU_ICON_URL))()
参考