『山と渓谷』の目次を取得するbookmarklet-2
表示されている全てのバックナンバーをscrapする
code:bookmarklet.js
code:script.js
javascript: (() => {
// utilities
const ng = text => text.trim().replace(/[\\\n]/g, ' '); const zero = n => String(n).padStart(2, '0');
const toYYYYMMDD = d => ${d.getFullYear()}-${zero(d.getMonth()+1)}-${zero(d.getDate())};
const toYYYYMMDD_HHMMSS = d => [${toYYYYMMDD(d)}] ${zero(d.getHours())}:${zero(d.getMinutes())}:${zero(d.getSeconds())};
const today = toYYYYMMDD_HHMMSS(new Date());
const e = text => encodeURIComponent(text);
const project = 'takker';
if(window.location.host + window.location.pathname !== 'www.fujisan.co.jp/product/5812/b/list/') return;
.map(dom => getMagazineData(dom));
for (const {title, category, imageURL, link, published, toc} of magazines) {
// scrap pageを作成する
const lines = [
[${imageURL} ${link}],
...toc.map(line => > ${ng(line)}),
'',
#${category}, // 一ヶ月前に発行される
Published on [${toYYYYMMDD(published)}],
Added on ${today},
];
window.open(https://scrapbox.io/${project}/${e(title)}?body=${e(lines.join('\n'))});
}
引用する範囲を取得する
code:script.js
function getMagazineData(cardBookDOM) {
// 目次を取得する
const toc = cardBookDOM.getElementsByClassName('card-book-toc')0 .innerText
.split(/\n\r?|\r?\n|\r|\f/g); // 改行区切りで配列化
// 発売日を取得する
.textContent
.replace(/.*発売日(\d+)年(\d+)月(\d+)日.*/, '$1-$2-$3')
.split('-').map(number => parseInt(number));
const published = new Date(year, month - 1, date);
let backnumberDate = new Date(published);
backnumberDate.setMonth(backnumberDate.getMonth() + 1); // 一ヶ月前に発行される
const title = 『山と渓谷 ${backnumberDate.getFullYear()}年${backnumberDate.getMonth() + 1}月号』;
// 書影を取得する
const imageURL = cardBookDOM.getElementsByClassName('img-responsive backnumber-image')0 .src.replace('_n', '_o');
const link = cardBookDOM.getElementsByClassName('card-book-name')0.firstElementChild.href; const category = 『山と渓谷』_${backnumberDate.getMonth() + 1}月号 ;
return {title, category, imageURL, link, published, toc};
}
})();