Random Page and Gallery for exhentai

adds random page and random gallery buttons to search pages

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

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

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         Random Page and Gallery for exhentai
// @namespace    http://exhentai.org/
// @version      1.1
// @description  adds random page and random gallery buttons to search pages
// @author       sllypper
// @include      *://exhentai.org/*
// @include      *://g.e-hentai.org/*
// @grant        none
// ==/UserScript==

// Options:

let openGalleryOnNewTab = true;

//

function onlyUnique(value, index, self) {
    return self.indexOf(value) === index;
}

function openRandGal() {
    let gals = Array.from(document.getElementsByClassName('itg')[0].getElementsByTagName('a')).filter(gal => gal.href.includes('/g/'));
    // gals = gals.filter(onlyUnique);
    window.open(gals[parseInt(Math.floor(Math.random() * gals.length - 1))].href, openGalleryOnNewTab ? '_blank' : '_parent');
}

function getRandPageNum() {
    let pageButtons = document.getElementsByClassName('ptt')[0].firstChild.firstChild.children;
    let numPages = pageButtons[pageButtons.length - 2].firstChild.text;
    return parseInt((numPages * Math.random()) - 1);
}

function openRandPage() {
    let pageNum = getRandPageNum();
    let url = Array.from(document.getElementsByClassName('ptt')[0].getElementsByTagName('a')).find(el => el.href.includes('page'));
    if (url) {
        url = url.href.replace(/(page=)\d+(&)/, '$1' + pageNum + '$2');
        window.open(url, '_parent');
    }
}

(function(){
    let isFavorites = location.href.indexOf('favorites') > 0;
    let place;
    let style = '';

    if (!isFavorites) {
        // on the main page
        place = document.getElementsByClassName('nopm');
        place = place[place.length - 1];
        style = 'float: right;'
    } else {
        // on the Favorites page
        place = document.getElementsByClassName('ip')[0];
    }

    let template = `<div id="randomStuff" style="${style}">
  <a id="randGalLink" href="#">Random Gallery</a> &nbsp; &nbsp;
  <a id="randPageLink" href="#">Random Page</a>
</div>`;

    place.innerHTML = !isFavorites ? place.innerHTML + template : template + place.innerHTML;

    var myLink = document.getElementById("randGalLink");
    myLink.addEventListener("click", openRandGal, true);
    
    var myPageLink = document.getElementById("randPageLink");
    myPageLink.addEventListener("click", openRandPage, true);
    
})();