🙸Imtely • Immediately Invoked Gallery🙶

Turn any iterable image URL into a live gallery at the same URL.

// ==UserScript==
// @name 🙸Imtely • Immediately Invoked Gallery🙶
// @description Turn any iterable image URL into a live gallery at the same URL. 
// @version 1.1
// @author Edvaldo
// @match *://*/*
// @run-at document-start
// @namespace http://tampermonkey.net/
// ==/UserScript==

/*
		The syntax of the self-executing Imtely URL

		IMAGE ACCESS
		_https://www.site.com/image/cat_01.jpg

		AUTO PREPARE PATH
		_https://www.site.com/image/cat_01.jpg?image/cat_01.jpg

		ENTER RANGE IN BRACKETS
		_https://www.site.com/image/cat_01.jpg?image/cat_[01-25].jpg

		OPTIONAL THUMB WITH IMAGE LINK
		_https://www.site.com/thumb/cat_01.jpg?thumb/cat_[01-25].jpg,thumb,image
		
		EXAMPLES
		https://dali-gallery.com/images/works/1936_01.jpg?images/works/1936_[01-15].jpg
		https://www.google.com/googlebooks/chrome/images/small/1.jpg?googlebooks/chrome/images/small/[1-38].jpg,small,big
*/

(function() {
    'use strict';
	if (!document.contentType?.startsWith("image")) {
        return
    }
    const q = new URL(location);
    if (!q.search) {
        q.search = `?${q.pathname.slice(1)}`;
        history.replaceState(null, "", q);
        return
    }
    if (/\[(\d+)-(\d+)\]/.test(q.search)) {
        let c = `
  <style>
    body{background:#000;padding:4pt;text-align:center}
    body:not(:hover) img{filter:none}
	img:not(:is([title])){display:none}
    img{margin:1pt;border:1pt solid #000;position:relative;max-width:256px;max-height:256px;vertical-align:middle;border-radius:8pt;filter:opacity(.9);transition:.1s}
    img:hover{z-index:1;filter:drop-shadow(0 0 3pt #000);transform:scale(1.1)}
  </style>
  <base href="${q.origin}" target="_blank">`;
        const o = `/${q.search.slice(1)}`.split("[")
          , m = o[1].split("]")
          , p = m[0].split("-")
          , e = m[1].split(",");
        for (let u = +p[0]; u <= +p[1]; u++) {
            const t = u.toString().padStart(p[0].length, 0)
              , i = o[0] + t + e[0]
              , l = e.length > 2 ? i.replace(e[1], e[2]) : i;
            c += `<a href="${l}"><img src="${i}"></a>`
        }
        document.documentElement.innerHTML = c;
        for (let r of document.images) {
            r.onerror = () => {
                r.parentElement?.remove();
                document.title = `${document.images.length}@${r.src.split("/")[2]}`
            }
            r.onload = () => {
                r.title = `𝗛𝗼𝘀𝘁⠆${r.src.split("/")[2]}\n𝗜𝗺𝗮𝗴𝗲⠆${decodeURIComponent(r.src.split("/").pop())}\n𝗦𝗶𝘇𝗲⠆${r.naturalWidth}×${r.naturalHeight} Pixel`;
                document.title = `${document.images.length}@${r.src.split("/")[2]}`
            }
        }
    }
	})();