// ==UserScript== // @name MyDealz | Kommentar-Export über alle Seiten // @namespace violentmonkey // @version 1.0 // @license MIT // @description Lädt alle Seiten, expandiert Antworten und exportiert Kommentare in ein neues Fenster mit Copy-All-Button // @match https://www.mydealz.de/deals/* // @match https://www.mydealz.de/diskussion/* // @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 = []; 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; } 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); } 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() {
    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() {
    const btn = document.createElement('button');
    btn.textContent = '💬 Kommentare exportieren';
    Object.assign(btn.style, {
      position: 'fixed',
      bottom: '20px',
      right: '20px',
      padding: '10px 16px',
      background: '#d32f2f',
      color: '#fff',
      border: 'none',
      borderRadius: '4px',
      fontSize: '14px',
      cursor: 'pointer',
      zIndex: '9999'
    });

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

    document.body.appendChild(btn);
  }

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