half-assed nHentai downloader

Download galleries from nHentai as a .zip file of images (webp/jpg/png), because their download button provides it as a torrent instead. I get that it offloads the bandwidth burden from their servers, but it's in no way the most user-friendly approach.

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

You will need to install an extension such as Tampermonkey 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         half-assed nHentai downloader
// @version      1.0
// @description  Download galleries from nHentai as a .zip file of images (webp/jpg/png), because their download button provides it as a torrent instead. I get that it offloads the bandwidth burden from their servers, but it's in no way the most user-friendly approach.
// @match        https://nhentai.net/g/*
// @grant        GM_xmlhttpRequest
// @connect      *.nhentai.net
// @connect      nhentai.net
// @require      https://cdn.jsdelivr.net/npm/[email protected]/umd/index.js
// @namespace https://greasyfork.org/users/1456281
// ==/UserScript==

// WOMM. If it breaks for you, that's your problem, not mine

/* globals fflate */

(function() {
    'use strict';

    const galleryData = unsafeWindow._gallery;
    const { id: galleryId, num_pages: pageCount, media_id: mediaId, images: { pages: imagePages } } = galleryData;

    const downloadButton = document.querySelector('#download.btn.btn-secondary');
    if (!downloadButton) return;

    const zipDownloadButton = Object.assign(document.createElement('a'), {
        className: 'btn btn-secondary',
        innerHTML: '<i class="fa fa-download"></i> ZIP',
    });

    downloadButton.parentNode.insertBefore(zipDownloadButton, downloadButton.nextSibling);

    const extensionMap = {
        'j': 'jpg',
        'p': 'png',
        'w': 'webp'
    };

    zipDownloadButton.onclick = () => {
        Promise.all(Array.from({ length: pageCount }, (_, i) => {
            const fileExtension = extensionMap[imagePages[i].t] || 'webp';
            return new Promise(resolve => {
                GM_xmlhttpRequest({
                    method: 'GET',
                    url: `https://i1.nhentai.net/galleries/${mediaId}/${i + 1}.${fileExtension}`,
                    responseType: 'arraybuffer',
                    onload: ({ response }) => {
                        resolve([`${i + 1}.${fileExtension}`, new Uint8Array(response)]);
                    },
                    onerror: (err) => {
                        console.error(`Failed to fetch page ${i + 1}.${fileExtension}:`, err);
                        resolve(null);
                    }
                });
            });
        })).then(imageFiles => {
            const validImageFiles = imageFiles.filter(file => file !== null);
            if (validImageFiles.length === 0) {
                console.log('tf?');
                return;
            }
            const zipArchive = fflate.zipSync(Object.fromEntries(validImageFiles), { level: 0 });
            const downloadLink = document.createElement('a');

            downloadLink.href = URL.createObjectURL(new Blob([zipArchive], { type: 'application/zip' }));
            downloadLink.download = `nhentai_${galleryId}.zip`;
            downloadLink.click();
            URL.revokeObjectURL(downloadLink.href);
        });
    };
})();