☰

F95 Random Bookmark Picker

Randomly picks a bookmark from your f95 bookmark page.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         F95 Random Bookmark Picker
// @namespace    1330126-edexal
// @license      Unlicense
// @version      2.1.3
// @description  Randomly picks a bookmark from your f95 bookmark page.
// @author       Edexal
// @match        *://f95zone.to/watched/threads?tab=bookmarks*
// @icon         https://external-content.duckduckgo.com/ip3/f95zone.to.ico
// @grant        GM.xmlhttpRequest
// @homepageURL  https://sleazyfork.org/en/scripts/490810-f95-random-bookmark-picker
// @supportURL   https://github.com/Edexaal/scripts/issues
// @require      https://cdn.jsdelivr.net/gh/Edexaal/scripts@2f4f36215375a30485901a0374cb58a78412ddf3/_lib/utility.js
// @require      https://cdn.jsdelivr.net/gh/Edexaal/scripts@2f4f36215375a30485901a0374cb58a78412ddf3/_lib/gm-utility.js
// @require      https://cdn.jsdelivr.net/gh/Edexaal/scripts@2f4f36215375a30485901a0374cb58a78412ddf3/_lib/css.js
// ==/UserScript==
(() => {
  'use strict';
  Edexal.addCSS(`
  #ed-rand-sect {
    & > div, & > li {
      background: #242629;
      outline: 2px solid #8c2d2d;
    }
    & > li {
      list-style: none;
    }
    & > button {
      text-align: center;
      width: 100%;
      font-weight: bold;
      margin-top: 10px;
      ${EdexalCSS.btnRules()}
      border-radius: 10px;
    }
  }
  `);
  const EL = {
    pageNav:'.pageNav ul li:last-child a', 
    bookmarks: '.p-body-pageContent .structItemContainer > div, ol.listPlain > li',
    chosenSect: "#ed-rand-sect > div, #ed-rand-sect > li"
  };
  const GLOBAL = {prevPageNum: null, prevBMIndex: null, cachePages: {}};
  
  function isGamesOrPostsOnlyPage() {
    return location.search.includes('bm=games') || location.search.includes('bm=posts');
  }
  
  function distinctRandomNumInc(min, max, globalObjName) {
    if (max <= min) return min;
    let pageNum;
    do {
      pageNum = Edexal.getRandIntInc(min, max);
    } while (pageNum === GLOBAL[globalObjName]);
    GLOBAL[globalObjName] = pageNum;
    return pageNum;
  }

  function getRandomPageNum() {
    const lastPage = Edexal.$(EL.pageNav); 
    const totalPages = !lastPage ? 1 : +lastPage.textContent;  // converts to Number
    return distinctRandomNumInc(1, totalPages, 'prevPageNum');
  }
  
  function getPageAsync(pageNum) {
    const url = new URL(location.href);
    url.searchParams.set('page', pageNum);
    return EdexalGM.getAsync(url.href);
  }
  
  function chooseBM(bmEls) {
    const chosenNum = distinctRandomNumInc(0, bmEls.length - 1, 'prevBMIndex');
    const chosenBM = bmEls[chosenNum].cloneNode(true);
    Edexal.$(EL.chosenSect).replaceWith(chosenBM);
  }
  
  async function randomBMEvent() {
    try {
      const pageNum = getRandomPageNum();
      let bookmarkEls = GLOBAL.cachePages[pageNum];
      if (!GLOBAL.cachePages[pageNum]) {
        const res = await getPageAsync(pageNum);
        bookmarkEls = res.response.querySelectorAll(EL.bookmarks);
        GLOBAL.cachePages[pageNum] = bookmarkEls;
      }
      chooseBM(bookmarkEls);
    } catch(err) {
      console.error(err);
    }
  }
  
  function addRandSection() {
    const container = Edexal.newEl({element: 'div', class: ['block'], id: 'ed-rand-sect'});
    let chosenSect = Edexal.$(EL.bookmarks);
    if (!chosenSect) return;
    chosenSect = chosenSect.cloneNode(true);
    if (chosenSect.nodeName === 'LI') {
      chosenSect.querySelector('label').remove();
    }
    const btn = Edexal.newEl({element: 'button', text: 'Random'});
    Edexal.on(btn, 'click', async () => await randomBMEvent());
    container.append(chosenSect, btn);
    Edexal.$('form.block').before(container);
  }
  
  function run() {
    if (!isGamesOrPostsOnlyPage()) return;
    addRandSection();
  }
  run();
})();