// ==UserScript== // @name MyDealz | Kommentar-Export über alle Seiten // @namespace violentmonkey // @version 1.1 // @description Lädt alle Seiten, expandiert Antworten und exportiert Kommentare mit Fortschrittsanzeige in Prozent // @match https://www.mydealz.de/deals/* // @match https://www.mydealz.de/diskussion/* // @match https://www.mydealz.de/gutscheine/* // @icon https://www.mydealz.de/assets/img/emojis/cool_b7b27.svg // @grant none // @downloadURL none // ==/UserScript== (function () { 'use strict'; const REPLY_BTN_SELECTOR = 'button[data-t="moreReplies"]:not([disabled])'; const NEXT_PAGE_SELECTOR = 'button[aria-label="Nächste Seite"]:not([disabled])'; const CURRENT_PAGE_SELECTOR = 'button[aria-label="Aktuelle Seite"]'; const LAST_PAGE_SELECTOR = 'button[aria-label="Letzte Seite"]'; const CHECK_INTERVAL = 500; const CLICK_DELAY = 200; const PAGE_DELAY = 2000; let collectedTexts = []; let totalComments = 0; let exportButton = null; async function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } function getCurrentPage() { const current = document.querySelector(CURRENT_PAGE_SELECTOR); if (!current) return null; const text = current.textContent.match(/\d+/); return text ? parseInt(text[0], 10) : null; } function getLastPage() { const last = document.querySelector(LAST_PAGE_SELECTOR); if (!last) return null; const text = last.textContent.match(/\d+/); return text ? parseInt(text[0], 10) : null; } function getTotalCommentCount() { const commentLink = document.querySelector('a[href*="#comments"]'); if (!commentLink) return 0; const text = commentLink.textContent.match(/\d+/); return text ? parseInt(text[0], 10) : 0; } async function expandAllReplies() { let checks = 0; while (checks < 5) { const btn = document.querySelector(REPLY_BTN_SELECTOR); if (btn && btn.offsetParent !== null) { btn.click(); checks = 0; await sleep(CLICK_DELAY); } else { checks++; await sleep(CHECK_INTERVAL); } } } function collectCommentBodies() { const nodes = document.querySelectorAll('.comment-body .userHtml-content'); const texts = Array.from(nodes).map(el => el.textContent.trim().replace(/\s+/g, ' ') ); collectedTexts.push(...texts); // Fortschritt anzeigen if (totalComments > 0 && exportButton) { const percent = Math.min(100, Math.round((collectedTexts.length / totalComments) * 100)); exportButton.textContent = `💬 ${percent}% geladen…`; } } async function goToNextPage() { const btn = document.querySelector(NEXT_PAGE_SELECTOR); if (btn) { btn.click(); await sleep(PAGE_DELAY); } } function openExportWindow(text) { const w = window.open('', '_blank', 'width=800,height=600'); if (!w) return alert('Popup blockiert'); w.document.title = 'MyDealz Kommentar-Export'; w.document.head.innerHTML = ` `; w.document.body.innerHTML = ` Copied!

    `;
    const pre = w.document.getElementById('exportText');
    pre.textContent = text;

    const btn = w.document.getElementById('copyBtn');
    const msg = w.document.getElementById('copiedMsg');
    btn.addEventListener('click', () => {
      w.navigator.clipboard.writeText(text).then(() => {
        msg.style.opacity = '1';
        setTimeout(() => {
          msg.style.opacity = '0';
        }, 2000);
      });
    });
  }

  async function runFullExport() {
    totalComments = getTotalCommentCount();
    console.log(`📊 Kommentar-Gesamtzahl: ${totalComments}`);

    while (true) {
      console.log(`🛠️ Seite ${getCurrentPage()} wird bearbeitet...`);
      await expandAllReplies();
      collectCommentBodies();

      const current = getCurrentPage();
      const last = getLastPage();

      if (current === null || last === null || current >= last) break;

      await goToNextPage();
    }

    const fullText = collectedTexts.join('\n\n');
    openExportWindow(fullText);
  }

  function injectStartButton() {
    exportButton = document.createElement('button');
    exportButton.textContent = '💬 Kommentare exportieren';
    Object.assign(exportButton.style, {
      position: 'fixed',
      top: '20px',
      right: '20px',
      padding: '10px 16px',
      background: '#d32f2f',
      color: '#fff',
      border: 'none',
      borderRadius: '4px',
      fontSize: '14px',
      cursor: 'pointer',
      zIndex: '9999'
    });

    exportButton.addEventListener('click', () => {
      exportButton.disabled = true;
      exportButton.textContent = '⏳ Läuft…';
      runFullExport().then(() => {
        exportButton.textContent = '✅ Fertig!';
      });
    });

    document.body.appendChild(exportButton);
  }

  function ensureStartOnPageOne() {
    const url = new URL(window.location.href);
    const isCommentPage = url.hash.includes('#comments');
    const pageParam = url.searchParams.get('page');

    if ((pageParam && pageParam !== '1') || !isCommentPage) {
      url.searchParams.set('page', '1');
      url.hash = '#comments';
      window.location.href = url.toString();
    } else {
      injectStartButton();
    }
  }

  if (document.readyState === 'complete') {
    ensureStartOnPageOne();
  } else {
    window.addEventListener('load', ensureStartOnPageOne);
  }
})();