順番に見返すUserScript
順番に見返すUserScript
各ページの関連ページからランダムに(今までに見たページと被らないように)選んで順番に見れると良いのかな
ふむ
script.js/** @type {URL[]} */
const urls = [];
/** @type {string} */
let startURL = "";
scrapbox.PageMenu.addItem({
title: "Random jump in relations",
onClick: () => {
if (startURL === "") {
/** @type {HTMLAnchorElement[]} */
const cards = shuffle(Array.from(
document.querySelectorAll(
"li.relation-label:not(.empty-links) ~ li.page-list-item a",
)
));
urls.push(
...cards.map((a) => new URL(a.href))
);
if (urls.length === 0) {
alert("no items");
return;
}
startURL = location.href;
}
if (urls.length === 0) {
alert("Back to the start page");
move(startURL);
startURL = "";
return;
}
move(urls.shift());
},
});
/** @param {string | URL} url */
function move(url) {
const a = document.createElement("a");
a.href = url;
document.body.append(a);
a.click();
a.remove();
}
/**
* @template T
* @param {T[]} array
* @returns {T[]}
*/
function shuffle(array) {
let result = [...array];
for (let i = result.length; 1 < i; i--) {
const k = Math.floor(Math.random() * i);
[result[k], result[i - 1]] = [result[i - 1], result[k]];
}
return result;
}