Greasy Fork

MyDealz Grafikpfad im Kommentar fixen

Korrigiert Bildpfade in Kommentaren und Antworten bei Klick auf das fehlende Bild

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

// ==UserScript==
// @name MyDealz Grafikpfad im Kommentar fixen
// @namespace http://tampermonkey.net/
// @version 1.2
// @description Korrigiert Bildpfade in Kommentaren und Antworten 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';
    const replyItemSelector = '.comment-replies-item';

    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 commentArticle = img.closest(commentArticleSelector);
        if (!commentArticle) return;

        // Prüfen ob wir in einer Antwort sind
        const replyItem = commentArticle.closest(replyItemSelector);
        const isReply = replyItem !== null;

        let commentId;

        if (isReply) {
            // Bei Antworten: ID direkt aus dem data-id Attribut oder aus dem id-Attribut ohne "reply-" Präfix
            if (replyItem.hasAttribute('data-id')) {
                commentId = replyItem.getAttribute('data-id');
            } else if (replyItem.id && replyItem.id.startsWith('reply-')) {
                commentId = replyItem.id.replace('reply-', '');
            } else {
                return; // Keine ID gefunden, Abbruch
            }
        } else {
            // Für Hauptkommentare: bisherige Logik
            const commentLi = commentArticle.closest(commentListItemSelector);
            if (!commentLi || !commentLi.id || !commentLi.id.startsWith('comment-')) return;
            commentId = commentLi.id.replace('comment-', '');
        }

        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;

        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);
})();