Raindrop Random Bookmark

Scroll through an open raindrop collection and pick a random bookmark.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Raindrop Random Bookmark
// @namespace    http://tampermonkey.net/
// @version      2026-02-07_02
// @description  Scroll through an open raindrop collection and pick a random bookmark.
// @author       AB
// @match        https://*.raindrop.io/*
// @icon         data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2280%22>🎲</text></svg>
// @grant        GM_registerMenuCommand
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    const getList = () => {
        const m = document.getElementsByTagName('main');
        return m.length > 1 ? m[1] : m[0];
    };

    const openItem = (idx, retries) => {
        const item = getList()?.childNodes[idx];
        if (!item) return retries > 0 && setTimeout(() => openItem(idx, retries - 1), 1000);

        const link = item.querySelector('a[class*="permalink"]');
        if (link?.href) return window.open(link.href, '_blank');

        if (retries > 0) {
            item.scrollIntoView({ behavior: 'smooth', block: 'center' });
            setTimeout(() => openItem(idx, retries - 1), 1000);
        }
    };

    const scrollList = () => {
        const footer = document.querySelector('[class*="footer"]');
        const list = getList();
        if (!list) return alert("Bookmark list not found.");

        if (footer?.childElementCount > 0) {
            list.lastElementChild?.scrollIntoView();
            setTimeout(scrollList, 100);
        } else {
            const total = parseInt(footer?.innerText.match(/\d+/)?.[0] || list.childElementCount);
            const count = Math.min(list.childElementCount, total);
            if (!count) return;

            const idx = Math.floor(Math.random() * count);
            list.childNodes[idx]?.scrollIntoView({ behavior: 'smooth', block: 'center' });
            setTimeout(() => openItem(idx, 10), 800);
        }
    };

    const addButton = () => {
        const share = document.querySelector('div[title="Share"][class*="button"]') ?? document.querySelector('div[title="Ask"][class*="button"]');
        if (!share) return setTimeout(addButton, 500);

        const btn = share.cloneNode(true);
        btn.title = 'Random';
        btn.onclick = scrollList;
        btn.querySelectorAll('*').forEach(n => {
            if (n.tagName === 'SPAN' && (n.innerText === 'Share' || n.innerText === 'Ask')) n.innerText = 'Random';
            else if (n.tagName === 'SPAN') {
                n.className = '';
                n.innerHTML = '⚃';
            }
        });
        share.parentNode.insertBefore(btn, share);
    };

    GM_registerMenuCommand("🎲 Open Random Bookmark", scrollList, "r");
    addButton();
})();