☰

F95 Random Bookmark Picker

Randomly picks a bookmark from your f95 bookmark page.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==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();
})();