// ==UserScript==
// @name Crack Chat Downloader (크랙 채팅 다운로더)
// @namespace https://github.com/kktcct001/crack_chat_downloader
// @version 2.4.7
// @description 크랙 캐릭터 채팅을 HTML, TXT, JSON 파일로 저장
// @author kktcct001
// @match https://crack.wrtn.ai/*
// @grant GM_addStyle
// @require https://cdn.jsdelivr.net/npm/marked@4.3.0/marked.min.js
// @require https://cdn.jsdelivr.net/npm/pako@2.1.0/dist/pako.min.js
// @downloadURL https://github.com/kktcct001/crack_chat_downloader/raw/refs/heads/main/Crack_Chat_Downloader.user.js
// @updateURL https://github.com/kktcct001/crack_chat_downloader/raw/refs/heads/main/Crack_Chat_Downloader.user.js
// ==/UserScript==
(function() {
'use strict';
const CONFIG = {
storageKey: 'CCD_lastTurnCount',
saveOrderKey: 'CCD_lastSaveOrder',
fullSaveDelay: 1000,
assistantBubbleColor: '#E9EFF5'
};
const SELECTORS = {
characterName: '.css-b7257o, .css-od8fa4',
buttons: {
desktopInjectContainer: '.css-l8r172',
mobileSidePanel: '.css-wcaza0, .css-114eyt3',
mobileScrollContent: '.css-c82bbp'
},
panel: {
overlay: '.downloader-panel-overlay',
closeBtn: '#downloader-close-btn',
statusText: '.status-text'
},
};
const ICONS = {
chat: ``,
close: ``,
arrowUp: ``,
arrowDown: ``,
edit: ``,
trash: ``,
unchecked: ``,
checked: ``,
list: ``,
panelToggle: ``,
spinner: ``,
success: ``,
error: ``,
search: ``,
journal: ``,
};
const apiHandler = {
apiBaseUrl: 'https://contents-api.wrtn.ai/character-chat/api/v2',
extractCookie(key) {
const match = document.cookie.match(new RegExp(`(?:^|; )${key.replace(/([.$?*|{}()[\]\\/+^])/g, '\\$1')}=([^;]*)`));
return match ? decodeURIComponent(match[1]) : null;
},
getChatInfo() {
const match = location.pathname.match(/\/stories\/([a-f0-9]+)\/episodes\/([a-f0-9]+)/);
return match ? {
characterId: match[1],
chatroomId: match[2]
} : null;
},
async fetchChatRoomDetail(chatroomId, accessToken) {
const url = `${this.apiBaseUrl}/chat-room/${chatroomId}`;
const response = await fetch(url, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
if (!response.ok) return null;
const data = await response.json();
return data.data || null;
},
async fetchAllChatrooms(accessToken, onPageLoad) {
let allRooms = [];
let nextCursor = null;
let pageCount = 1;
let hasMore = true;
const headers = {
'Authorization': `Bearer ${accessToken}`
};
while (hasMore) {
if (onPageLoad) onPageLoad(pageCount, allRooms.length);
const url = nextCursor ? `${this.apiBaseUrl}/chat?type=character&limit=40&cursor=${nextCursor}` : `${this.apiBaseUrl}/chat?type=character&limit=40`;
const response = await fetch(url, {
headers
});
if (!response.ok) throw new Error(`서버 응답 오류: ${response.status}`);
const responseData = await response.json();
if (!responseData?.data?.chats) throw new Error('API 응답에서 채팅 목록(data.chats)을 찾을 수 없습니다.');
const {
chats,
nextCursor: newCursor
} = responseData.data;
if (chats.length > 0) allRooms.push(...chats);
if (newCursor) {
nextCursor = newCursor;
pageCount++;
} else {
hasMore = false;
}
}
return allRooms;
},
async fetchAllMessages(chatroomId, accessToken) {
// [턴 상한 수정 가이드 1/3]
// 아래의 'limit=2000'은 한 번에 불러올 메시지의 최대 개수 (2000개 = 1000턴)
// 만약 턴 수 상한을 2000턴으로 올리고 싶다면, 값을 'limit=4000'으로 변경
// [!주의!] 4000개(2000턴) 정도 권장, 값을 너무 높이면 서버에서 요청을 거부할 수 있음
const url = `${this.apiBaseUrl}/chat-room/${chatroomId}/messages?limit=2000`;
const response = await fetch(url, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
if (!response.ok) throw new Error(`'${chatroomId}' 메시지 로드 실패: ${response.status}`);
const data = await response.json();
// [임시 해결 로직]
const originalMessages = data.data.list || [];
const uniqueMessages = [];
const seenIds = new Set();
for (const message of originalMessages) {
if (!seenIds.has(message._id)) {
seenIds.add(message._id);
uniqueMessages.push(message);
}
}
// 중복 제거 후 정상 배열 반환
return uniqueMessages;
}
};
const baseStyles = `
:root {
--surface_chat_secondary: #61605A;
--text_white: #fff;
--text_primary: #1A1918;
--text_secondary: #61605A;
--text_tertiary: #85837D;
--text_disabled: #C7C5BD;
--icon_tertiary: #85837D;
--icon_white: #fff;
--point_red: #FF4432;
}
body { font-family: "Pretendard", "Apple SD Gothic Neo", sans-serif; margin: 0; }
.message-wrapper { display: flex; flex-direction: column; margin-bottom: 15px; }
.message-wrapper.user { align-items: flex-end; }
.message-wrapper.assistant { align-items: flex-start; }
.character-name-wrapper { display: flex; }
.character-name { font-size: 14px; color: var(--text_secondary); margin-bottom: 5px; padding-left: 10px; }
.message-bubble { position: relative; line-height: 1.6; word-wrap: break-word; box-sizing: border-box; }
.user-bubble { padding: 12px 20px; border-radius: 10px 10px 0 10px; background-color: var(--surface_chat_secondary); color: var(--text_white); max-width: 640px; }
.assistant-bubble { padding: 16px 20px; border-radius: 0 10px 10px; background-color: ${CONFIG.assistantBubbleColor}; color: var(--text_primary); max-width: 740px; }
div, p { margin-bottom: 1em; }
div:last-child, p:last-child { margin-bottom: 0; }
.message-bubble h1, .message-bubble h2, .message-bubble h3 { color: var(--text_primary); font-weight: 700; }
.user-bubble h1, .user-bubble h2, .user-bubble h3 { color: var(--text_white); }
.message-bubble ul, .message-bubble ol { padding: 4px 0 4px 20px; line-height: 180%; font-weight: 500; list-style-position: outside; }
.user-bubble ul, .user-bubble ol { color: var(--text_white); }
.assistant-bubble ul, .assistant-bubble ol { color: var(--text_primary); }
.message-bubble strong { font-weight: 700; }
.user-bubble strong { color: var(--text_white); }
.assistant-bubble strong { color: var(--text_primary); }
.message-bubble em { font-style: normal; }
.assistant-bubble em { color: var(--text_tertiary); }
.user-bubble em { color: var(--text_disabled); }
.message-bubble blockquote { margin: 10px 0; padding: 10px 15px; border-left: 4px solid #ccc; background-color: #f9f9f9; color: #666; }
.message-bubble img { max-width: 100%; border-radius: 5px; }
.message-bubble pre { background-color: #2d2d2d; color: #f2f2f2; padding: 15px; border-radius: 5px; white-space: pre-wrap; font-family: monospace; }
.message-bubble hr { border: none; border-top: 1px solid #ddd; margin: 1.5em 0; }
.floating-buttons { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 1002; }
.floating-btn { width: 40px; height: 40px; border-radius: 50%; background-color: #333; color: #fff; border: none; cursor: pointer; font-size: 24px; display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 5px rgba(0, 0, 0, .2); }
.floating-btn svg { width: 20px; height: 20px; }
.un-modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, .6); z-index: 2000; display: flex; justify-content: center; align-items: center; }
.un-modal-content { background-color: #fff; padding: 24px; border-radius: 8px; width: 90%; max-width: 500px; max-height: 80%; display: flex; flex-direction: column; }
.un-modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; font-size: 18px; font-weight: 700; }
.un-modal-close-btn { background: 0 0; border: none; cursor: pointer; font-size: 24px; }
.un-modal-body { overflow-y: auto; white-space: pre-wrap; line-height: 1.6; font-size: 14px; color: #1A1918; background-color: #f8f9fa; padding: 12px; border-radius: 4px; }
.un-modal-body::-webkit-scrollbar { width: 8px; }
.un-modal-body::-webkit-scrollbar-track { background-color: transparent; }
.un-modal-body::-webkit-scrollbar-thumb { background-color: #85837D; border-radius: 4px; }
.un-modal-body::-webkit-scrollbar-thumb:hover { background-color: #61605A; }
@media (max-width: 768px) {
body { padding-bottom: 80px; }
body.edit-mode .floating-buttons, body.panel-open-mob .floating-buttons { opacity: 0; visibility: hidden; pointer-events: none; transition: none; }
.floating-buttons.init-hide { opacity: 0; visibility: hidden; pointer-events: none; }
.floating-buttons.visible { opacity: 1; visibility: visible; pointer-events: auto; transition: opacity .3s, bottom .3s, visibility .3s, pointer-events 0s .3s; }
.floating-btn { width: 50px; height: 50px; font-size: 28px; }
.floating-btn svg { width: 24px; height: 24px; }
}
@media (max-width: 840px) {
body { font-size: 13px; }
.chat-container { padding: 10px 5px; }
.user-bubble, .assistant-bubble { max-width: 100%; border-radius: 8px; }
.message-bubble { font-size: 1em; }
.message-bubble h1 { font-size: 1.5em; }
.message-bubble h2 { font-size: 1.3em; }
.message-bubble h3 { font-size: 1.15em; }
.message-wrapper.user, .message-wrapper.assistant { align-items: stretch; }
}
`;
const currentChatStyle = `
body { background-color: #fff; padding-bottom: 80px; }
body.edit-mode { padding-bottom: 60px; }
.chat-container { max-width: 800px; margin: 0 auto; padding: 20px; display: flex; flex-direction: column; }
.user-bubble { padding: 12px 20px 36px; }
.assistant-bubble { padding: 16px 20px 36px; }
body.edit-mode .message-bubble { cursor: pointer; }
.floating-buttons { transition: opacity .3s, bottom .3s, visibility .3s, pointer-events 0s .3s; }
body.edit-mode .floating-buttons { bottom: 80px; }
.floating-btn:hover { opacity: .8; }
.message-actions { position: absolute; bottom: 8px; right: 8px; width: 24px; height: 24px; display: flex; justify-content: center; align-items: center; }
.action-btn, .message-checkbox { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: none; justify-content: center; align-items: center; background-color: transparent; border: none; padding: 0; cursor: pointer; box-sizing: border-box; }
.action-btn svg { width: 20px; height: 20px; }
.message-checkbox svg { width: 16px; height: 16px; }
.user-bubble .action-btn svg, .user-bubble .message-checkbox svg { fill: var(--icon_white); }
.assistant-bubble .action-btn svg, .assistant-bubble .message-checkbox svg { fill: var(--icon_tertiary); }
body:not(.edit-mode) .delete-btn { display: flex; }
body.edit-mode .message-checkbox { display: flex; }
.checkbox-icon.checked { display: none; }
.message-wrapper.selected .checkbox-icon.checked { display: block; }
.message-wrapper.selected .checkbox-icon.unchecked { display: none; }
.delete-confirm-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,.5); display: flex; justify-content: center; align-items: center; z-index: 1003; }
.delete-confirm-panel { display: flex; flex-direction: column; padding: 32px 24px 20px; width: 320px; background-color: #fff; border-radius: 10px; text-align: center; }
.delete-confirm-panel .text-group { flex: 1; display: flex; flex-direction: column; justify-content: center; gap: 8px; padding-bottom: 16px; }
.delete-confirm-panel .title { color: #1a1918; font-size: 18px; font-weight: 700; margin: 0; }
.delete-confirm-panel .subtitle { color: #61605a; font-size: 14px; margin: 0; }
.delete-confirm-buttons { display: flex; width: 100%; gap: 8px; }
.delete-confirm-buttons button { flex: 1; padding: 0 20px; height: 40px; border-radius: 8px; font-size: 16px; font-weight: 600; cursor: pointer; border: none; }
.delete-confirm-cancel { background-color: #f0efeb; color: #1a1918; }
.delete-confirm-delete { background-color: #0d0d0c; color: #fcfcfa; }
.save-changes-container { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 1000; display: none; }
.save-changes-btn { padding: 12px 24px; border-radius: 100px; border: none; background-color: #FF4432; color: #fff; font-size: 16px; font-weight: 700; cursor: pointer; box-shadow: 0 4px 12px rgba(0,0,0,.2); }
#edit-action-bar { position: fixed; bottom: 0!important; left: 0; width: 100%; background-color: #333; color: #fff; display: none; align-items: center; padding: 12px 20px!important; margin: 0!important; box-sizing: border-box; z-index: 1001; }
body.edit-mode #edit-action-bar { display: flex; }
#selection-count { font-size: 16px; font-weight: 600; flex-grow: 1; text-align: center; margin-left: auto; }
.action-bar-buttons { display: flex; gap: 8px; align-items: center; margin-left: auto; }
.action-bar-btn { background: 0 0; border: none; color: #fff; cursor: pointer; padding: 8px; display: flex; align-items: center; justify-content: center; }
.action-bar-btn svg { width: 24px; height: 24px; }
#bulk-delete-btn { opacity: 1; transition: opacity .2s; }
#bulk-delete-btn:disabled { opacity: .5; cursor: not-allowed; }
@media (min-width: 769px) {
#bulk-delete-btn svg, #exit-edit-mode-btn svg { width: 28px; height: 28px; }
}
@media (max-width: 768px) {
body.edit-mode { padding-bottom: 60px; }
}
`;
const fullChatStyle = `
body { background-color: #F8F9FA; transition: padding-left .3s ease-in-out; }
body.panel-open-mob { overflow: hidden; }
#main-chat-view { max-width: 800px; margin: 0 auto; padding: 20px 20px 80px; }
.assistant-bubble { box-shadow: 0 1px 3px rgba(0, 0, 0, .05); }
#chat-list-panel { position: fixed; top: 0; left: 0; width: 260px; height: 100%; background-color: #F7F7F5; border-right: 1px solid #e9ecef; transform: translateX(-100%); transition: transform .3s ease-in-out; z-index: 1002; display: flex; flex-direction: column; }
#chat-list-panel.is-open { transform: translateX(0); }
.panel-header { display: flex; align-items: center; justify-content: space-between; padding: 16px; font-size: 16px; font-weight: 600; border-bottom: 1px solid #e9ecef; flex-shrink: 0; margin-bottom: 0; }
.panel-header .title { font-size: 16px; font-weight: 700; color: var(--text_primary); }
.panel-header-buttons { display: flex; align-items: center; gap: 12px; }
.header-btn { background: none; border: none; padding: 0; cursor: pointer; display: flex; align-items: center; justify-content: center; }
.header-btn svg { width: 16px; height: 16px; fill: #1A1918; }
.header-btn:disabled svg { fill: #85837D; }
.search-bar-container { max-height: 0; overflow: hidden; transition: max-height .3s ease-in-out, padding .3s ease-in-out, border-bottom-width .3s ease-in-out; padding: 0 16px; border-bottom: 0px solid #e9ecef; margin-bottom: 0 !important; box-sizing: border-box; }
.search-bar-container.open { max-height: 52px; padding: 10px 16px; border-bottom-width: 1px; }
#chat-search-input { width: 100%; padding: 8px 12px; border-radius: 6px; border: 1px solid #dee2e6; background-color: #FFFFFF; font-size: 14px; box-sizing: border-box; }
#chat-search-input:focus { outline: none; border-color: #61650A; }
.panel-scroll-area { overflow-y: auto; flex-grow: 1; padding: 8px; }
.panel-scroll-area::-webkit-scrollbar { width: 8px; }
.panel-scroll-area::-webkit-scrollbar-track { background-color: transparent; }
.panel-scroll-area::-webkit-scrollbar-thumb { background-color: #85837D; border-radius: 4px; }
.panel-scroll-area::-webkit-scrollbar-thumb:hover { background-color: #61605A; }
.chat-list-item { display: flex; align-items: center; padding: 10px 12px; text-decoration: none; color: inherit; cursor: pointer; border-radius: 8px; margin-bottom: 4px; background-color: transparent; transition: background-color 0.2s ease-in-out; }
.chat-list-item:hover { background-color: #FFFFFF; }
.chat-list-item.active { background-color: #FFFFFF; }
.chat-list-item.hidden { display: none; }
.list-item-avatar { width: 36px; height: 36px; border-radius: 50%; overflow: hidden; margin-right: 12px; flex-shrink: 0; }
.list-item-avatar img { width: 100%; height: 100%; object-fit: cover; }
.list-item-content { overflow: hidden; }
.list-item-name { font-weight: 600; font-size: 14px; color: var(--text_primary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin: 0 0 4px; }
.list-item-topic { font-size: 13px; color: var(--text_secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin: 0; }
#panel-toggle-btn { position: fixed; top: 50%; left: 0; transform: translateY(-50%); width: 24px; height: 60px; background-color: #f7f7f5; border: 1px solid #e9ecef; border-left: none; border-radius: 0 4px 4px 0; cursor: pointer; z-index: 1000; display: flex; align-items: center; justify-content: center; padding: 0; transition: left .3s ease-in-out; overflow: hidden; box-shadow: 0 2px 8px rgba(0, 0, 0, .12); }
body.panel-open-pc #panel-toggle-btn { left: 260px; z-index: 1002; }
#panel-toggle-btn svg { width: 20px; height: 20px; position: relative; }
#panel-toggle-btn #toggle_bar { transition: transform .3s ease-in-out; }
#panel-toggle-btn #toggle_open_arrow, #panel-toggle-btn #toggle_close_arrow { position: absolute; opacity: 0; transition: transform .3s ease-in-out, opacity .3s ease-in-out; }
body:not(.panel-open-pc) #panel-toggle-btn #toggle_open_arrow { transform: translateX(-12px); }
body:not(.panel-open-pc) #panel-toggle-btn:hover #toggle_bar { transform: translateX(12px); }
body:not(.panel-open-pc) #panel-toggle-btn:hover #toggle_open_arrow { opacity: 1; transform: translateX(0); }
body.panel-open-pc #panel-toggle-btn #toggle_close_arrow { transform: translateX(12px); }
body.panel-open-pc #panel-toggle-btn:hover #toggle_bar { transform: translateX(0); }
body.panel-open-pc #panel-toggle-btn:hover #toggle_close_arrow { opacity: 1; transform: translateX(0); }
@media (min-width: 769px) {
body.panel-open-pc { padding-left: 260px; }
#mobile-list-btn { display: none; }
}
@media (max-width: 768px) {
#panel-toggle-btn { display: none; }
#chat-list-panel { box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1); }
body.panel-open-pc { padding-left: 0; }
#main-content-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, .5); z-index: 1000; display: none; opacity: 0; transition: opacity .3s ease-in-out; }
body.panel-open-mob #main-content-overlay { display: block; opacity: 1; }
}
`;
const contentGenerator = {
generateTxt(messages) {
return messages.map(msg => `[${msg.role === 'user' ? 'user' : 'assistant'} message]\n\n${msg.content}`).join('\n\n===\n\n');
},
generateJson(messages) {
const filtered = messages.map(msg => ({
role: msg.role,
content: msg.content
}));
return JSON.stringify(filtered, null, 2);
},
generateHtml(messages, characterName, userNote) {
const renderer = new marked.Renderer();
renderer.heading = (text, level) => `
${quote}`; renderer.hr = () => '
${code.replace(//g, '>')}`;
delete renderer.paragraph;
marked.setOptions({
renderer,
gfm: true,
breaks: true
});
const messageHtml = messages.map(msg => {
let contentHtml = marked.parse(msg.content || '');
contentHtml = contentHtml.replace(//g, `
선택한 메시지를 삭제하시겠습니까?