On Stage

Make images load at light-speed

Tendrás que instalar una extensión para tu navegador como Tampermonkey, Greasemonkey o Violentmonkey si quieres utilizar este script.

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

Tendrás que instalar una extensión como Tampermonkey o Violentmonkey para instalar este script.

Necesitarás instalar una extensión como Tampermonkey o Userscripts para instalar este script.

Tendrás que instalar una extensión como Tampermonkey antes de poder instalar este script.

Necesitarás instalar una extensión para administrar scripts de usuario si quieres instalar este script.

(Ya tengo un administrador de scripts de usuario, déjame instalarlo)

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Tendrás que instalar una extensión como Stylus antes de poder instalar este script.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

Para poder instalar esto tendrás que instalar primero una extensión de estilos de usuario.

(Ya tengo un administrador de estilos de usuario, déjame instalarlo)

// ==UserScript==
// @name         On Stage
// @namespace    https://tampermonkey.net/
// @version      1.0
// @description  Make images load at light-speed
// @license      MIT
// @match        https://msoe.instructure.com/*
// @icon         https://msoe.instructure.com/favicon.ico
// @grant        unsafeWindow
// @run-at       document-start
// @inject-into  page
// ==/UserScript==

(() => {
    'use strict';

    /* -----------------------------
       Indexed DB
    ------------------------------ */
    const dbp = new Promise(res => {
        const r = indexedDB.open('tm-img-cache', 1);
        r.onupgradeneeded = () => r.result.createObjectStore('i');
        r.onsuccess = () => res(r.result);
    });

    const get = async k => {
        const db = await dbp;
        return new Promise(r => {
            const q = db.transaction('i').objectStore('i').get(k);
            q.onsuccess = () => r(q.result || null);
        });
    };

    const set = async (k, v) => {
        const db = await dbp;
        db.transaction('i', 'readwrite').objectStore('i').put(v, k);
    };

    /* -----------------------------
       URL Normalization
    ------------------------------ */
    const norm = url => {
        try {
            const u = new URL(url, location.href);
            u.search = '';
            u.hash = '';
            return u.href;
        } catch {
            return url;
        }
    };

    /* -----------------------------
       Blob Loader
    ------------------------------ */
    async function blobURL(url) {
        const key = norm(url);
        const cached = await get(key);
        if (cached) return URL.createObjectURL(cached);

        const r = await fetch(url, { cache: 'force-cache' });
        const b = await r.blob();
        set(key, b);
        return URL.createObjectURL(b);
    }

    /* -----------------------------
      Src / SrcSet / SetAttribute
    ------------------------------ */
    const imgProto = HTMLImageElement.prototype;

    const realSrc = Object.getOwnPropertyDescriptor(imgProto, 'src');
    const realSetAttr = Element.prototype.setAttribute;

    Object.defineProperty(imgProto, 'src', {
        configurable: true,
        set(v) {
            if (!v || v.startsWith('blob:')) {
                realSrc.set.call(this, v);
                return;
            }
            blobURL(v).then(b => realSrc.set.call(this, b));
        },
        get() {
            return realSrc.get.call(this);
        }
    });

    Object.defineProperty(imgProto, 'srcset', {
        configurable: true,
        set(v) {
            if (!v) return;
            const first = v.split(',')[0].trim().split(' ')[0];
            this.src = first;
        }
    });

    Element.prototype.setAttribute = function (k, v) {
        if (this instanceof HTMLImageElement && k === 'src') {
            this.src = v;
            return;
        }
        return realSetAttr.call(this, k, v);
    };

    /* -----------------------------
       Background Images
    ------------------------------ */
    async function lockBG(el) {
        const bg = getComputedStyle(el).backgroundImage;
        if (!bg || !bg.includes('url(')) return;

        const url = bg.match(/url\(["']?(.*?)["']?\)/)?.[1];
        if (!url || url.startsWith('blob:')) return;

        const b = await blobURL(url);
        el.style.backgroundImage = `url("${b}")`;
    }

    const scan = root => {
        root.querySelectorAll('img').forEach(img => {
            if (img.getAttribute('src'))
                img.src = img.getAttribute('src');
        });
        root.querySelectorAll('*').forEach(lockBG);
    };

    new MutationObserver(m => {
        for (const x of m) {
            x.addedNodes.forEach(n => n.nodeType === 1 && scan(n));
            if (x.target?.nodeType === 1) lockBG(x.target);
        }
    }).observe(document, { childList: true, subtree: true, attributes: true });

    scan(document);

})();