Greasy Fork

MyDealz Grafikpfad im Kommentar fixen

Korrigiert Bildpfade in Kommentaren bei Klick auf das fehlende Bild

当前为 2025-04-25 提交的版本,查看 最新版本

// ==UserScript==
// @name         MyDealz Grafikpfad im Kommentar fixen
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Korrigiert Bildpfade in Kommentaren bei Klick auf das fehlende Bild
// @author       MD928835
// @license      MIT
// @match        https://www.mydealz.de/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const commentListItemSelector = 'li.commentList-item';
    const commentArticleSelector = 'article.comment';
    const commentBodySelector = '.comment-body .userHtml-content';

    function fixClickedImage(img) {
        const originalSrc = img.getAttribute('src');
        const dataImageId = img.dataset.image;

        if (!originalSrc || !dataImageId || !originalSrc.includes('static.mydealz.de/comments/') || originalSrc.split('/').pop().includes('_')) {
            return;
        }

        const srcMatch = originalSrc.match(/\/(\d+)(\.[^.\/]+)$/);
        if (!srcMatch || srcMatch[1] !== dataImageId) {
             return;
        }

        const commentLi = img.closest(commentListItemSelector);
        if (!commentLi || !commentLi.id || !commentLi.id.startsWith('comment-')) {
            return;
        }
        const commentId = commentLi.id.replace('comment-', '');

        const commentArticle = img.closest(commentArticleSelector);
        const commentBody = commentArticle?.querySelector(commentBodySelector);
        if (!commentBody) {
             return;
        }

        const imagesInComment = Array.from(commentBody.querySelectorAll('img[src*="static.mydealz.de/comments/"][data-image]'));
        let imgIndex = 0;
        let currentIdx = 0;
        for (const siblingImg of imagesInComment) {
            const siblingSrc = siblingImg.getAttribute('src');
            const siblingDataImageId = siblingImg.dataset.image;
            const siblingFileName = siblingSrc?.split('/').pop();
            const siblingSrcMatch = siblingSrc?.match(/\/(\d+)(\.[^.\/]+)$/);

            if (siblingSrc && siblingDataImageId && !siblingFileName.includes('_') && siblingSrcMatch && siblingSrcMatch[1] === siblingDataImageId) {
                currentIdx++;
                if (siblingImg === img) {
                    imgIndex = currentIdx;
                    break;
                }
            }
        }

        if (imgIndex === 0) {
            imgIndex = 1; // Fallback
        }

        const newName = `${commentId}_${imgIndex}`;
        let newSrc = originalSrc.replace(/\/(\d+)\/fs\//, `/${newName}/fs/`);
        newSrc = newSrc.replace(/\/(\d+)(\.[^.\/]+)$/, `/${newName}$2`);

        if (newSrc !== originalSrc) {
            img.setAttribute('src', newSrc);
            if (img.dataset.src === originalSrc) {
                img.dataset.src = newSrc;
            }
            if (img.srcset && img.srcset.includes(originalSrc)) {
                 const oldSrcEscaped = originalSrc.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
                 img.srcset = img.srcset.replace(new RegExp(oldSrcEscaped, 'g'), newSrc);
            }
            img.removeEventListener('click', handleClick);
        }
    }

    function handleClick(event) {
        if (event.target.tagName === 'IMG') {
             const img = event.target;
             const src = img.getAttribute('src');
             if (src && src.includes('static.mydealz.de/comments/') && img.dataset.image && !src.split('/').pop().includes('_')) {
                 if (img.naturalWidth === 0) {
                     fixClickedImage(img);
                 }
             }
        }
    }

    document.body.addEventListener('click', handleClick);

})();