Scrapboxにページを指定して追記するTextwellアクションの中身
code:script.js
// xxxxx にプロジェクトを記述
// Textwell for Mac で使用する場合は sbporter の部分を https へ書き換える
const homeUrl = 'sbporter://scrapbox.io/xxxxx/';
// 追記するページのタイトルと追記文の前後に追加するテキストのリスト
// 1つ以上いくつでもOK 2つ以上だと選択肢が出る
const itemList = [];
itemList.push({
title: 'あんなページ',
prepend: '',
append: ''
});
itemList.push({
title: '_yyyy_/_MM_/_dd_(_E_) 日記',
prepend: '_HH_:_mm_\n',
append: ''
});
// 実行後のオプション
const option = 'clearAll'; // 本文をクリア
// const option = 'none'; // クリアしない
// 1日の境目 午前0時なら0 午前4時なら4
// ページ名だけに適用される
const borderHour = 0;
/* ページ名と本文の追加テキストに使用できる日時の変数
2017年1月2日(月)午前3時4分に実行した場合
_yyyy_ : 2017
_yy_ : 17
_MM_ : 01
_M_ : 1
_dd_ : 02
_d_ : 2
_EEE_: Monday
_EE_ : Mon
_E_ : 月
_HH_ : 03
_H_ : 3
_mm_ : 04
_m_ : 4
*/
const now = new Date();
const today = (() => {
const today = new Date();
const border = new Date(today.getFullYear(), today.getMonth(), today.getDate(), borderHour, 0, 0);
if (today < border) {
today.setDate(today.getDate() - 1);
}
return today;
})();
const titleBodyList = itemList.map(item => {
const result = {};
result.title = replaceDateString(item.title, today);
result.body = replaceDateString(item.prepend, now) + T.text + replaceDateString(item.append, now);
return result;
});
if (titleBodyList.length === 1) {
const url = homeUrl + encodeURIComponent(titleBodyList0.title) + '?body=' + encodeURIComponent(titleBodyList0.body); T('urlScheme', { url: url, option: option });
} else {
T.loadlets(makeObjList(titleBodyList), function() {
location.href = 'about:blank';
});
}
function makeObjList(titleBodyList) {
return titleBodyList.map(item => {
const result = {};
result.title = item.title;
result.fn = function(arg) {
const url = arg.homeUrl + encodeURIComponent(arg.pageTitle) + '?body=' + encodeURIComponent(arg.body);
T('urlScheme', { url: url, option: arg.option });
};
result.arg = { homeUrl: homeUrl, pageTitle: item.title, body: item.body, option: option };
return result;
});
}
function replaceDateString(text, date = new Date()) {
if (text === '') return '';
const year = date.getFullYear();
const year2digits = year.toString().slice(-2);
const month = date.getMonth() + 1;
const monthPad = ('0' + month).slice(-2);
const day = date.getDate();
const dayPad = ('0' + day).slice(-2);
const hour = date.getHours();
const hourPad = ('0' + hour).slice(-2);
const minute = date.getMinutes();
const minutePad= ('0' + minute).slice(-2);
const replacePairs = {
'_yyyy_': year,
'_yy_': year2digits,
'_MM_': monthPad,
'_M_': month,
'_dd_': dayPad,
'_d_': day,
'_EEE_': weekdayFull,
'_EE_': weekdayShort,
'_E_': youbi,
'_HH_': hourPad,
'_H_': hour,
'_mm_': minutePad,
'_m_': minute
};
const reg = new RegExp('(' + Object.keys(replacePairs).join('|') + ')', 'g');
return text.replace(reg, key => replacePairskey); }