Better HentaiPaw

Fullscreen image viewer for HentaiPaw

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Better HentaiPaw
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Fullscreen image viewer for HentaiPaw
// @author       Eric4
// @match        https://zh.hentaipaw.com/viewer?articleId=*&page=*
// @grant        none
// @license      GNU GPLv3
// ==/UserScript==

(function() {
    'use strict';

    let currentIndex = 0;
    let imageLinks = [];

    // Function to extract image links
    function extractImageLinks() {
        console.log("Extracting image links...");
        const regex = /https:\/\/cdn\.imagedeliveries\.com\/.*?\.webp/g;
        const matches = document.body.innerHTML.match(regex);

        if (matches && matches.length > 0) {
            console.log("Found image links: ", matches);
            imageLinks = matches;

            if (imageLinks.length > 0) {
                displayImage(imageLinks[currentIndex]);
            }
        } else {
            console.log("No image links found.");
        }
    }

    // Function to display image
    function displayImage(src) {
        console.log("Displaying image: ", src);
        document.body.innerHTML = '';
        document.body.style.backgroundColor = 'black';
        document.body.style.margin = '0';
        document.body.style.display = 'flex';
        document.body.style.justifyContent = 'center';
        document.body.style.alignItems = 'center';
        document.body.style.height = '100vh';
        document.body.style.overflow = 'hidden';

        const img = document.createElement('img');
        img.src = src;
        img.style.maxWidth = '100%';
        img.style.maxHeight = '100%';
        img.style.objectFit = 'contain'; // Ensures the image fits within the container without stretching

        document.body.appendChild(img);

        preloadImages(currentIndex + 1, 3); // Preload next 3 images
    }

    // Function to preload images
    function preloadImages(startIndex, count) {
        for (let i = startIndex; i < startIndex + count; i++) {
            if (i < imageLinks.length) {
                const img = new Image();
                img.src = imageLinks[i];
                console.log("Preloading image: ", imageLinks[i]);
            }
        }
    }

    // Function to increase image number
    function nextImage() {
        if (currentIndex < imageLinks.length - 1) {
            currentIndex++;
            displayImage(imageLinks[currentIndex]);
        }
    }

    // Function to decrease image number
    function prevImage() {
        if (currentIndex > 0) {
            currentIndex--;
            displayImage(imageLinks[currentIndex]);
        }
    }

    // Function to get the article ID from the URL
    function getArticleId() {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get('articleId');
    }

    // Extract image links when the page loads
    window.addEventListener('load', extractImageLinks);

    // Add event listeners for key press 'd', 'a' and 'Esc'
    document.addEventListener('keydown', (e) => {
        if (e.key === 'd') {
            nextImage();
        } else if (e.key === 'a') {
            prevImage();
        } else if (e.key === 'Escape') {
            const articleId = getArticleId();
            if (articleId) {
                window.location.href = `https://zh.hentaipaw.com/articles/${articleId}`;
            }
        }
    });
})();