Pixiv Image Preload

Preload pixiv images

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

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

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.

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

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        Pixiv Image Preload
// @namespace   https://greatest.deepsurf.us/en/users/37676
// @description Preload pixiv images
// @match       *://*.pixiv.net/member_illust.php?*mode=manga*
// @match       *://*.pixiv.net/member_illust.php?*mode=medium*
// @match       *://*.pixiv.net/*/artworks/*
// @run-at      document-end
// @version     2.0.0
// @grant       none
// @license     Creative Commons Attribution 4.0 International Public License; http://creativecommons.org/licenses/by/4.0/
// ==/UserScript==

var preloadMeta = document.querySelector('meta[name="preload-data"]');

if (preloadMeta)
{
	var preloadContent = preloadMeta.getAttribute('content');

    if (preloadContent)
    {
        var illustID = null;

        try
        {
            preloadContent = JSON.parse(preloadContent);
            illustID = Object.values(preloadContent.illust)[0].id;

        } catch(e) {

        }
		
        if (illustID)
        {
            fetch('https://www.pixiv.net/ajax/illust/'+illustID+'/pages?lang=en').then((response) => {
                return response.json();
            }).then((imagesObj) => {
                if (imagesObj.error == false && Array.isArray(imagesObj.body))
                {
                    var arrayImage = [];

                    for (const element of imagesObj.body)
                    {
                        if (element.urls.regular)
                        arrayImage.push(element.urls.regular);
                    }

                    if (arrayImage.length > 0)
                    loadImage(0, arrayImage);
                }

            }).catch(function(error) {
                console.log("error fetching json data");
                console.log(error);
            });
        }
    }
}

function loadImage(index, arrayImage)
{
	if (index < arrayImage.length)
	{
		var image = new Image();
		image.onload = function() {
			loadImage(index+1, arrayImage);
		};
		image.src = arrayImage[index];
	}
}