MangaPark Broken Image Fix (s01 server)

Replace s02..s10.mp* par s01.mp* pour les images qui ne chargent pas sur MangaPark

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         MangaPark Broken Image Fix (s01 server)
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Replace s02..s10.mp* par s01.mp* pour les images qui ne chargent pas sur MangaPark
// @author       Toi + GPT
// @match        *://mangapark.net/*
// @match        *://mangapark.org/*
// @match        *://mangapark.io/*
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const badServers = ['s02', 's03', 's04', 's05', 's06', 's07', 's08', 's09', 's10'];

    function replaceServersInString(str) {
        if (!str) return str;
        let newStr = str;
        badServers.forEach(s => {
            // remplace juste le début https://s0X.mp par https://s01.mp
            newStr = newStr.replace(`https://${s}.mp`, 'https://s01.mp');
        });
        return newStr;
    }

    function fixImg(img) {
        if (!img) return;

        // src direct
        if (img.src) {
            const fixed = replaceServersInString(img.src);
            if (fixed !== img.src) img.src = fixed;
        }

        // attributs de lazy-load fréquents
        const lazyAttrs = ['data-src', 'data-original', 'data-lazy'];
        lazyAttrs.forEach(attr => {
            const val = img.getAttribute(attr);
            if (!val) return;
            const fixed = replaceServersInString(val);
            if (fixed !== val) img.setAttribute(attr, fixed);
        });

        // srcset (plusieurs URLs)
        const srcsetAttrs = ['srcset', 'data-srcset'];
        srcsetAttrs.forEach(attr => {
            const val = img.getAttribute(attr);
            if (!val) return;

            const fixed = val
                .split(',')
                .map(part => {
                    const trimmed = part.trim();
                    if (!trimmed) return trimmed;
                    const bits = trimmed.split(/\s+/); // "url 1x"
                    bits[0] = replaceServersInString(bits[0]);
                    return bits.join(' ');
                })
                .join(', ');

            if (fixed !== val) img.setAttribute(attr, fixed);
        });
    }

    function fixAllImages(root = document) {
        const imgs = root.querySelectorAll('img');
        imgs.forEach(fixImg);
    }

    // 1) Au chargement de la page
    fixAllImages();

    // 2) Quand de nouveaux éléments apparaissent (scroll, changement de chapitre, etc.)
    const observer = new MutationObserver(mutations => {
        for (const mutation of mutations) {
            mutation.addedNodes.forEach(node => {
                if (!(node instanceof HTMLElement)) return;

                if (node.tagName === 'IMG') {
                    fixImg(node);
                } else {
                    fixAllImages(node);
                }
            });
        }
    });

    if (document.body) {
        observer.observe(document.body, { childList: true, subtree: true });
    }

    // Option debug : déscommente si tu veux vérifier que le script tourne
    // console.log('[MangaPark Fix] Script actif, images traitées:', document.images.length);
})();