Save NHentai images to Eagle

go to the manga selection page and then just right click and then save. Happy fapping! (VPN required)

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Save NHentai images to Eagle
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  go to the manga selection page and then just right click and then save. Happy fapping! (VPN required)
// @author       GreasyWeebs
// @match        *://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @run-at       context-menu
// @require      https://code.jquery.com/jquery-3.5.1.min.js
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    const EAGLE_SERVER_URL = "http://localhost:41595";
    const EAGLE_IMPORT_API_URL = `${EAGLE_SERVER_URL}/api/item/addFromURL`;
    const EAGLE_CREATE_FOLDER_API_URL = `${EAGLE_SERVER_URL}/api/folder/create`;

    const SELECTOR_IMAGE = ".gallery a";
    const SELECTOR_HIGHRES = ".thumb-container noscript img";
    var imgElements = Array.from(document.querySelectorAll(SELECTOR_IMAGE));
    var galleryElements = Array.from(document.querySelectorAll('.gallery'));
    var galleryCount = galleryElements.length;
    var count = 0;

    var createFolder = function(folderName, callback) {
        GM_xmlhttpRequest({
            url: EAGLE_CREATE_FOLDER_API_URL,
            method: "POST",
            data: JSON.stringify({ folderName: folderName }),
            onload: function(response) {
                try {
                    var result = JSON.parse(response.response);
                    if (result.status === "success" && result.data && result.data.id) {
                        callback(undefined, result.data);
                    } else {
                        callback(true);
                    }
                } catch (err) {
                    callback(true);
                }
            }
        });
    };

    function addImageToEagle(data) {
        GM_xmlhttpRequest({
            url: EAGLE_IMPORT_API_URL,
            method: "POST",
            data: JSON.stringify(data),
            onload: function (response) {
            }
        });
    }

    function saveImages(folder, title, imgCount, hiresImgID, tagsData, imgLinkSource){
        for(var i=0;i<imgCount;i++){
            var saveData = {
                "url": "https://i.nhentai.net/galleries/"+hiresImgID+"/"+(i+1)+".jpg",
                "name": 'page '+(i+1),
                "website": imgLinkSource,
                "folderId": folder.id,
                "tags": tagsData,
                "headers": {
                     "referer": "nhentai.net"
                }
            };
            addImageToEagle(saveData);
        }
    }

    var fetchImg = setInterval(function(){
        $.get(imgElements[count].href, function (html) {
            var parser = new DOMParser();
            var doc = parser.parseFromString(html, "text/html");
            var preloadData = Array.from(doc.querySelectorAll('.thumb-container'));
            var imgCount = preloadData.length;
            var titleRaw = Array.from(doc.querySelectorAll('.title'));
            var title = titleRaw[0].querySelector('.title .before').innerHTML+" "+titleRaw[0].querySelector('.title .pretty').innerHTML+" "+titleRaw[0].querySelector('.title .after').innerHTML;
            var hiresImg = Array.from(doc.querySelectorAll(SELECTOR_HIGHRES));
            var hiresImgSplit1 = hiresImg[0].src.split("https://t.nhentai.net/galleries/");
            var hiresImgSplit2 = hiresImgSplit1[1].split("/1t");
            var hiresImgID = hiresImgSplit2[0];
            var tags = Array.from(doc.querySelectorAll('.tag-container .tags a .name'));
            var imgLinkSource = imgElements[count].href;
            var tagsData = [];
            for(var i=0;i<tags.length-1;i++){
                tagsData[i] = tags[i].innerHTML;
            }
            createFolder(title, function (err, folder) {
            if (folder) {
                saveImages(folder, title, imgCount, hiresImgID, tagsData, imgLinkSource);
            } else {
                alert("folder is not exist");
            }
        });
        });
        count++;
        if(count==galleryCount){
            clearInterval(fetchImg);
        }
    }, 1000);
})();