-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenhanced-conversation-saver-final.js
252 lines (219 loc) · 9.22 KB
/
enhanced-conversation-saver-final.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// ==UserScript==
// @name 增强版三合一会话记录保存
// @namespace http://tampermonkey.net/
// @version 2.0
// @description 集成JSON下载、Markdown转换和Unicode字符清理的增强版会话记录保存工具,支持多种模型
// @author You
// @match https://chatgpt.com/c/*
// @match https://abcd.dwai.world/c/*
// @match https://ed.dawuai.buzz/c/*
// @match https://node.dawuai.buzz/c/*
// @match https://newnode.dawuai.buzz/c/*
// @match https://france.dwai.work/c/*
// @match https://dw.dwai.world/c/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 状态追踪
let state = {
largestResponse: null,
largestResponseSize: 0,
largestResponseUrl: null,
lastUpdateTime: null,
markdownContent: null,
modelName: null // 新增:模型名称
};
// 日志函数
const log = {
info: (msg) => console.log(`[Conversation Saver] ${msg}`),
error: (msg, e) => console.error(`[Conversation Saver] ${msg}`, e)
};
// Unicode清理函数
function removeSpecificUnicode(text) {
const regex = /[\uE203\uE204]/gu;
return text.replace(regex, '');
}
// 新增:提取模型名称
function extractModelName(text) {
try {
const data = JSON.parse(text);
const mapping = data.mapping || {};
for (const key in mapping) {
const message = mapping[key].message;
if (message?.metadata?.model_slug) {
return message.metadata.model_slug;
}
}
return null;
} catch (e) {
log.error('提取模型名称时出错:', e);
return null;
}
}
// JSON到Markdown转换相关函数
[... 保持原有的Markdown转换相关函数不变 ...]
// 响应处理函数
function processResponse(text, url) {
try {
const responseSize = text.length;
if (responseSize > state.largestResponseSize) {
state.largestResponse = text;
state.largestResponseSize = responseSize;
state.largestResponseUrl = url;
state.lastUpdateTime = new Date().toLocaleTimeString();
state.modelName = extractModelName(text); // 新增:提取模型名称
// 预处理Markdown内容
try {
const cleanedJson = removeSpecificUnicode(text);
const jsonData = JSON.parse(cleanedJson);
convertToMarkdown(jsonData);
} catch (e) {
log.error('预处理Markdown内容时出错:', e);
}
updateButtonsStatus();
log.info(`发现更大的响应 (${responseSize} bytes) 来自: ${url}`);
}
} catch (e) {
log.error('处理响应时出错:', e);
}
}
// 监听 fetch 请求
[... 保持原有的请求监听代码不变 ...]
// 更新按钮状态
function updateButtonsStatus() {
const modelInfo = state.modelName ? `\n模型: ${state.modelName}` : ''; // 新增:模型信息
const commonStatus = state.largestResponse
? `最后更新: ${state.lastUpdateTime}\n来源: ${state.largestResponseUrl}\n大小: ${(state.largestResponseSize / 1024).toFixed(2)}KB${modelInfo}`
: '等待响应中...';
const jsonButton = document.getElementById('jsonDownloadButton');
const mdButton = document.getElementById('mdDownloadButton');
if (jsonButton) {
jsonButton.style.backgroundColor = state.largestResponse ? '#28a745' : '#007bff';
jsonButton.title = commonStatus;
}
if (mdButton) {
mdButton.style.backgroundColor = state.markdownContent ? '#28a745' : '#007bff';
mdButton.title = state.markdownContent
? commonStatus + '\nMarkdown转换已就绪'
: commonStatus + '\nMarkdown转换未就绪';
}
}
// 创建按钮容器和按钮
function createButtons() {
const containerStyles = {
position: 'fixed',
top: '45%',
right: '0px',
transform: 'translateY(-50%)',
zIndex: '9999',
display: 'flex',
flexDirection: 'column',
gap: '5px',
padding: '5px',
backgroundColor: 'rgba(255, 255, 255, 0.9)',
borderRadius: '5px',
boxShadow: '0 2px 5px rgba(0,0,0,0.2)',
};
const buttonStyles = {
padding: '10px',
backgroundColor: '#007bff',
color: '#ffffff',
border: 'none',
borderRadius: '5px',
cursor: 'pointer',
transition: 'all 0.3s ease',
fontFamily: 'Arial, sans-serif',
whiteSpace: 'nowrap',
width: '120px',
textAlign: 'center',
};
const container = document.createElement('div');
Object.assign(container.style, containerStyles);
// JSON下载按钮
const jsonButton = document.createElement('button');
jsonButton.id = 'jsonDownloadButton';
jsonButton.innerText = '下载JSON';
Object.assign(jsonButton.style, buttonStyles);
// Markdown下载按钮
const mdButton = document.createElement('button');
mdButton.id = 'mdDownloadButton';
mdButton.innerText = '下载MD';
Object.assign(mdButton.style, buttonStyles);
// 添加悬停效果
[jsonButton, mdButton].forEach(button => {
button.onmouseover = () => {
button.style.transform = 'scale(1.05)';
button.style.boxShadow = '0 4px 8px rgba(0,0,0,0.3)';
};
button.onmouseout = () => {
button.style.transform = 'scale(1)';
button.style.boxShadow = 'none';
};
});
// 下载功能
jsonButton.onclick = function() {
if (!state.largestResponse) {
alert('还没有发现有效的会话记录。\n请等待页面加载完成或进行一些对话。');
return;
}
try {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5);
const chatName = document.title.trim().replace(/[/\\?%*:|"<>]/g, '-');
const modelSuffix = state.modelName ? `_${state.modelName}` : ''; // 新增:模型后缀
const fileName = `${chatName}${modelSuffix}_${timestamp}.json`;
const cleanedJson = removeSpecificUnicode(state.largestResponse);
const blob = new Blob([cleanedJson], { type: 'application/json' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = fileName;
link.click();
log.info(`成功下载JSON文件: ${fileName}`);
} catch (e) {
log.error('下载JSON过程中出错:', e);
alert('下载过程中发生错误,请查看控制台了解详情。');
}
};
mdButton.onclick = function() {
if (!state.markdownContent) {
alert('Markdown内容未就绪。\n请确保已有有效的会话记录。');
return;
}
try {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5);
const chatName = document.title.trim().replace(/[/\\?%*:|"<>]/g, '-');
const modelSuffix = state.modelName ? `_${state.modelName}` : ''; // 新增:模型后缀
const fileName = `${chatName}${modelSuffix}_${timestamp}.md`;
const blob = new Blob([state.markdownContent], { type: 'text/markdown' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = fileName;
link.click();
log.info(`成功下载Markdown文件: ${fileName}`);
} catch (e) {
log.error('下载Markdown过程中出错:', e);
alert('下载过程中发生错误,请查看控制台了解详情。');
}
};
container.appendChild(jsonButton);
container.appendChild(mdButton);
document.body.appendChild(container);
updateButtonsStatus();
}
// 页面加载完成后初始化
window.addEventListener('load', function() {
createButtons();
// 使用 MutationObserver 确保按钮始终存在
const observer = new MutationObserver(() => {
if (!document.getElementById('jsonDownloadButton') || !document.getElementById('mdDownloadButton')) {
log.info('检测到按钮丢失,正在重新创建...');
createButtons();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
log.info('增强版三合一会话保存脚本已启动');
});
})();