nhentai auto scroller

Automatically changes the image source and page counter every 6 seconds (toggle button now included)

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

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

Tendrás que instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Tendrás que instalar una extensión como Tampermonkey antes de poder instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name         nhentai auto scroller
// @namespace    https://nhentai.net/
// @version      1.3
// @description  Automatically changes the image source and page counter every 6 seconds (toggle button now included)
// @match        https://nhentai.net/g/*/*/
// @author       equmaq
// @grant        none
// @license      MIT
// ==/UserScript==


(function() {
    'use strict';

    // Function to change the image source
    function changeImageSource() {
        if (document.hidden) {
            return; // Skip changing the source if the page is not in focus
        }

        var toggleButton = document.evaluate("/html/body/div[2]/section[4]/div[2]/span", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        if (toggleButton.textContent === "►") {
            return; // Skip changing the source if the toggle button is displaying "►"
        }

        var imgElement = document.evaluate("/html/body/div[2]/section[3]/a/img", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        var currentSrc = imgElement.src;
        var splitSrc = currentSrc.split('/');
        var currentNumber = splitSrc[splitSrc.length - 1].split('.')[0];
        var newNumber = parseInt(currentNumber) + 1;
        var newSrc = currentSrc.replace(currentNumber + '.jpg', newNumber + '.jpg');
        imgElement.src = newSrc;
    }

    // Function to change the page counter text
    function changePageCounter() {
        if (document.hidden) {
            return; // Skip changing the page counter if the page is not in focus
        }

        var toggleButton = document.evaluate("/html/body/div[2]/section[4]/div[2]/span", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        if (toggleButton.textContent === "►") {
            return; // Skip changing the page counter if the toggle button is displaying "►"
        }

        var counterElement = document.evaluate("/html/body/div[2]/section[4]/div[2]/button/span[1]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        var currentPage = parseInt(counterElement.textContent);
        var newPage = currentPage + 1;
        counterElement.textContent = newPage.toString();
    }

    // Function to toggle the button text and action
    function toggleButton() {
        if (document.hidden) {
            return; // Skip toggling the button if the page is not in focus
        }

        var buttonElement = document.createElement("span");
        buttonElement.style.width = "40px";
        buttonElement.style.height = "40px";
        buttonElement.style.display = "inline-flex";
        buttonElement.style.alignItems = "center";
        buttonElement.style.justifyContent = "center";
        buttonElement.style.cursor = "pointer";
        buttonElement.style.fontSize = "24px";
        buttonElement.style.userSelect = "none";
        buttonElement.textContent = "►"; // Set initial button text

        var toggleButton = true;

        function toggle() {
            toggleButton = !toggleButton;
            buttonElement.textContent = toggleButton ? "►" : "II";
        }

        buttonElement.addEventListener("click", toggle);

        var containerElement = document.evaluate("/html/body/div[2]/section[4]/div[2]", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        containerElement.appendChild(buttonElement);
    }

    // Function to recreate the toggle button element
    function recreateToggleButton() {
        var toggleButton = document.evaluate(
            "/html/body/div[2]/section[4]/div[2]/span",
            document,
            null,
            XPathResult.FIRST_ORDERED_NODE_TYPE,
            null
        ).singleNodeValue;

        if (!toggleButton) {
            toggleButton = document.createElement("span");
            toggleButton.style.width = "40px";
            toggleButton.style.height = "40px";
            toggleButton.style.display = "inline-flex";
            toggleButton.style.alignItems = "center";
            toggleButton.style.justifyContent = "center";
            toggleButton.style.cursor = "pointer";
            toggleButton.style.fontSize = "24px";
            toggleButton.style.userSelect = "none";
            toggleButton.textContent = "►";

            var containerElement = document.evaluate(
                "/html/body/div[2]/section[4]/div[2]",
                document,
                null,
                XPathResult.FIRST_ORDERED_NODE_TYPE,
                null
            ).singleNodeValue;
            containerElement.appendChild(toggleButton);
        }
    }

    // Mutation observer callback function
    function mutationCallback(mutationsList) {
        for (var mutation of mutationsList) {
            if (mutation.type === 'childList') {
                recreateToggleButton();
            }
        }
    }

    // Create a new MutationObserver instance
    var observer = new MutationObserver(mutationCallback);

    // Start observing changes in the target node
    observer.observe(document.body, { childList: true, subtree: true });

    // Call the functions every 6 seconds
    setInterval(changeImageSource, 6000);
    setInterval(changePageCounter, 6000);
    toggleButton();
    recreateToggleButton();

})();