MangaPark Broken Image Fix (s01 server)

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
})();