script.js(function () {
const tables = [
'https://scrapbox.io/api/table/mrsekut-b/いくつかのスクボからページをランダムに表示する/projects.csv'
];
const randomChoiceFrom = arr => {
const idx = Math.floor(Math.random() * arr.length);
return arr[idx];
};
scrapbox.PageMenu.addMenu({
title: 'ランダムジャンプ',
image: 'https://dummyimage.com/200x200/ffffff/8f088f&text=ランダム',
onClick: async () => {
new Promise((resolve, reject) => {
fetch(randomChoiceFrom(tables))
.then(res => res.text())
.then(resolve)
.catch(reject);
}).then(text => {
const projects = text
.split('\n')
.map(line => line.split(',')[0].replace(/\[|\]/g, ''));
const project = randomChoiceFrom(projects);
const limit = 100;
const skip = 0;
fetch(`/api/pages${project}?limit=${limit}&skip=${skip}`)
.then(res => res.json())
.then(({ pages }) => {
const titles = pages.map(p => p.title);
const title = randomChoiceFrom(titles);
window.open(`https://scrapbox.io${project}/${title}`);
})
.catch(err => {
console.error('fetch failed', err);
});
});
}
});
})();