Greasy Fork

MyDealz Grafikpfad im Kommentar fixen

Korrigiert Bildpfade in MyDealz Kommentaren automatisch

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

// ==UserScript==
// @name         MyDealz Grafikpfad im Kommentar fixen
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Korrigiert Bildpfade in MyDealz Kommentaren automatisch
// @author       MD928835
// @license      MIT
// @match        https://www.mydealz.de/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // --- Selektoren ---
    const optionsButtonSelector = '.button--square.button--size-s.button--mode-default.button--type-tertiary.button--shape-circle.button.space--mr-1';
    const commentHeaderSelector = 'article.comment .comment-header';
    const commentArticleSelector = 'article.comment';
    const commentListItemSelector = 'li.commentList-item';
    const commentBodySelector = '.comment-body .userHtml-content';
    // --- Ende Selektoren ---

    /**
     * Sucht und korrigiert Bildpfade im angegebenen Kommentar-Element.
     * @param {Element} commentArticle Das article-Element des Kommentars.
     * @param {string} commentId Die ID des Kommentars.
     * @returns {number} Die Anzahl der korrigierten Bilder.
     */
    function fixCommentImages(commentArticle, commentId) {
        const commentBody = commentArticle.querySelector(commentBodySelector);
        if (!commentBody) {
            return 0;
        }

        // Nur Bilder von static.mydealz.de/comments/ mit data-image Attribut prüfen
        const images = commentBody.querySelectorAll('img[src*="static.mydealz.de/comments/"][data-image]');
        if (images.length === 0) {
             return 0; // keine Bilder gefunden
        }

        let imgCount = 1;
        let imagesChanged = 0;

        images.forEach(img => {
            const originalSrc = img.getAttribute('src');
            const dataImageId = img.dataset.image;

            if (!originalSrc || !dataImageId) return;

            const srcMatch = originalSrc.match(/\/(\d+)(\.[^.\/]+)$/);
            const filenameContainsUnderscore = originalSrc.split('/').pop().includes('_');

            if (srcMatch && srcMatch[1] === dataImageId && !filenameContainsUnderscore) {
                const newName = `${commentId}_${imgCount}`;
                let newSrc = originalSrc.replace(/\/(\d+)\/fs\//, `/${newName}/fs/`);
                newSrc = newSrc.replace(/\/(\d+)(\.[^.\/]+)$/, `/${newName}$2`);

                if (newSrc !== originalSrc) {
                    img.setAttribute('src', newSrc);
                    // Optional: srcset und data-src anpassen
                     if (img.dataset.src === originalSrc) img.dataset.src = newSrc;
                     if (img.srcset && img.srcset.includes(originalSrc)) {
                         img.srcset = img.srcset.replace(new RegExp(originalSrc.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), "g"), newSrc);
                     }
                    imagesChanged++;
                    imgCount++;
                }
            }
        });

        return imagesChanged;
    }

    document.body.addEventListener('click', function(event) {
        const clickedButton = event.target.closest(optionsButtonSelector);

        if (clickedButton && clickedButton.closest(commentHeaderSelector)) {
            const originatingButton = clickedButton;

            // Finde Kontext für die Bildkorrektur
            const commentArticle = originatingButton.closest(commentArticleSelector);
            const commentLi = commentArticle?.closest(commentListItemSelector);
            const commentId = commentLi?.id.replace('comment-', '');

            if (commentArticle && commentId) {
                fixCommentImages(commentArticle, commentId);
            }

        }
    });

})();