generated at
未読のページにランダムジャンプするUserScript

import.scrapbox
[未読のページにランダムジャンプするUserScript] code:script.js import '/api/code/yosider-scripts/未読のページにランダムジャンプするUserScript/script.js';


script.js
const jumpToUnreadPage = 'Jump to an unread page'; scrapbox.PageMenu.addMenu({ title: jumpToUnreadPage, image: 'https://gyazo.com/f74d4fc373248fb2abac31ff1f69b482/raw', onClick: async () => { const project = scrapbox.Project.name; const pages = scrapbox.Project.pages.filter( page => page.exists && page.title !== scrapbox.Page.title ); const getNextPage = async () => { const title = pages[Math.floor(Math.random() * pages.length)].title; const nextPage = await fetch(`/api/pages/${project}/${encodeURIComponent(title)}`); return await nextPage.json(); } const menu = scrapbox.PageMenu(jumpToUnreadPage); menu.removeAllItems(); menu.emitChange(); let count = 0; while (true) { const nextPage = await getNextPage(); const msg = `${++count}: /${project}/${nextPage.title}`; menu.addItem({ title: msg, onClick: () => {} }); console.log(msg); const unreadLine = nextPage.lines.find(line => nextPage.lastAccessed < line.updated); if (unreadLine) { const a = document.createElement('a'); a.href = `./${encodeURIComponent(nextPage.title)}#${unreadLine.id}`; a.rel = 'route'; document.body.appendChild(a); a.click(); a.remove(); return; } } }, });
location hrefの代わりに <a rel="route"> を使ってジャンプすることで、SPAを維持できる
元: if (unreadLine) { location.href = /${project}/${encodeURIComponent(nextPage.title)}#${unreadLine.id}; }

全ページ既読だと無限ループする