SpicyChat Chat Export

Export full SpicyChat chat log

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

You will need to install an extension such as Tampermonkey to install this script.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         SpicyChat Chat Export
// @version      1.2
// @description  Export full SpicyChat chat log
// @match        https://spicychat.ai/*
// @grant        GM_download
// @grant        GM_setClipboard
// @namespace https://greasyfork.org/users/1537524
// ==/UserScript==

(function() {
  'use strict';

  function htmlToText(html) {
    return html
      .replace(/<em[^>]*>(.*?)<\/em>/g, '*$1*')
      .replace(/<strong[^>]*>(.*?)<\/strong>/g, '**$1**')
      .replace(/<q[^>]*>(.*?)<\/q>/g, '"$1"')
      .replace(/<blockquote[^>]*>(.*?)<\/blockquote>/g, '`$1`')
      .replace(/<br\s*\/?>/gi, '\n')
      .replace(/<[^>]+>/g, '');
  }

  function decode(str) {
    const div = document.createElement('div');
    div.innerHTML = str;
    return div.textContent || '';
  }

  function parseMessage(node) {
    const spans = node.querySelectorAll('span');
    const textParts = [];
    spans.forEach(span => {
      const raw = htmlToText(span.innerHTML.trim());
      const txt = decode(raw).trim();
      if (txt) textParts.push(txt);
    });
    return textParts.join('\n\n');
  }

  function collect() {
    // Only top-level bubbles
    const bubbles = document.querySelectorAll(
      'div.flex.flex-col.justify-undefined.items-undefined.gap-md.w-full.px-\\[13px\\].py-md'
    );
    const seen = new Set();
    const lines = [];

    bubbles.forEach(bubble => {
      const nameEl = bubble.querySelector('p.font-sans, a p.font-sans');
      const speaker = nameEl ? nameEl.textContent.trim() : 'Unknown';
      const contentEl = bubble.querySelector('div.flex.flex-col.w-full');
      if (!contentEl) return;
      const msg = parseMessage(contentEl);
      const key = speaker + '|' + msg.slice(0, 100); // simple duplicate key
      if (!seen.has(key) && msg) {
        seen.add(key);
        lines.push(`--- ${speaker.toUpperCase()} ---\n\n${msg}`);
      }
    });

    const finalText = lines.join('\n\n');
    GM_setClipboard(finalText);
    const blob = new Blob([finalText], { type: 'text/plain;charset=utf-8' });
    GM_download({
      url: URL.createObjectURL(blob),
      name: `SpicyChat_Export_${new Date().toISOString().slice(0,19).replace(/:/g,'-')}.txt`,
      saveAs: true,
    });
//    alert('✅ Export complete. Copied to clipboard.');
  }

  function addBtn() {
    if (document.getElementById('exportChatBtn')) return;
    const btn = document.createElement('button');
    btn.textContent = '📄 Export Chat';
    Object.assign(btn.style, {
      position: 'fixed',
      bottom: '20px',
      right: '20px',
      zIndex: 9999,
      background: '#0ea5e9',
      color: '#fff',
      border: 'none',
      borderRadius: '8px',
      padding: '8px 12px',
      fontSize: '13px',
      cursor: 'pointer',
      boxShadow: '0 4px 10px rgba(0,0,0,0.2)'
    });
    btn.id = 'exportChatBtn';
    btn.onclick = collect;
    document.body.appendChild(btn);
  }

  window.addEventListener('load', addBtn);
  const mo = new MutationObserver(addBtn);
  mo.observe(document.body, { childList: true, subtree: true });
})();