Greasy Fork

MyDealz | Kommentar-Export über alle Seiten

Lädt alle Seiten, expandiert Antworten und exportiert Kommentare in ein neues Fenster mit Copy-All-Button

当前为 2025-05-15 提交的版本,查看 最新版本

// ==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
// ==/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 = `
      <style>
        body { font-family: sans-serif; margin: 20px; }
        #copyBtn {
          display: inline-flex; align-items: center;
          padding: 10px 16px; background: #d32f2f; color: #fff;
          border: none; border-radius: 4px; font-size: 16px;
          cursor: pointer; margin-bottom: 12px;
        }
        #copyBtn:active { background: #b71c1c; }
        #copiedMsg {
          display: inline-block; margin-left: 12px;
          color: #4caf50; font-weight: bold; opacity: 0;
          transition: opacity 0.3s;
        }
        pre {
          white-space: pre-wrap; word-wrap: break-word;
          border: 1px solid #ccc; padding: 12px; border-radius: 4px;
          max-height: 80vh; overflow: auto;
        }
      </style>
    `;
    w.document.body.innerHTML = `
      <button id="copyBtn" title="In Zwischenablage kopieren">
        📋 Copy All
      </button>
      <span id="copiedMsg">Copied!</span>
      <pre id="exportText"></pre>
    `;
    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);
})();