Random Page and Gallery for exhentai

adds random page and random gallery buttons to search pages

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==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);
    
})();