-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
938 lines (841 loc) · 35.2 KB
/
Copy pathcontent.js
File metadata and controls
938 lines (841 loc) · 35.2 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
// Chat2File-Gemini - Google Gemini 对话导出工具
// 支持单条导出和批量导出
(function() {
'use strict';
console.log('[Chat2File-Gemini] 脚本开始执行');
// ============================================
// Gemini DOM 选择器
// ============================================
const SELECTORS = {
AI_RESPONSE: '.response-container, .response-container-with-gpi, .presented-response-container',
// 只使用一个选择器,避免嵌套匹配
USER_MESSAGE: '.user-query-container',
MESSAGE_CONTENT: '.response-content, .model-response-text, .markdown',
THINKING: '.model-thoughts, .thoughts-container',
ACTION_BAR: '.actions-container-v2, .buttons-container-v2, .response-footer',
CHAT_CONTAINER: 'main, [role="main"], .chat-container'
};
// ============================================
// 全局状态
// ============================================
let isSelectionMode = false;
let selectedMessages = new Set();
let exportedMessages = new Map();
// ============================================
// 工具函数
// ============================================
function log(msg) { console.log('[Chat2File-Gemini]', msg); }
function warn(msg) { console.warn('[Chat2File-Gemini]', msg); }
// ============================================
// HTML 转 Markdown 转换器
// ============================================
function htmlToMarkdown(html) {
if (!html) return '';
const temp = document.createElement('div');
temp.innerHTML = html;
const removeSelectors = ['script', 'style', 'nav', 'header', 'footer'];
removeSelectors.forEach(sel => {
temp.querySelectorAll(sel).forEach(el => el.remove());
});
function convertNode(node) {
if (node.nodeType === Node.TEXT_NODE) return node.textContent;
if (node.nodeType !== Node.ELEMENT_NODE) return '';
const tag = node.tagName.toLowerCase();
const children = Array.from(node.childNodes).map(convertNode).join('');
switch (tag) {
case 'h1': return '\n# ' + children + '\n\n';
case 'h2': return '\n## ' + children + '\n\n';
case 'h3': return '\n### ' + children + '\n\n';
case 'h4': return '\n#### ' + children + '\n\n';
case 'p': return children + '\n\n';
case 'br': return '\n';
case 'strong': case 'b': return '**' + children + '**';
case 'em': case 'i': return '*' + children + '*';
case 'code': return '`' + children + '`';
case 'pre': return '\n```\n' + children + '\n```\n\n';
case 'a': return '[' + children + '](' + (node.getAttribute('href') || '') + ')';
case 'ul': return '\n' + children + '\n';
case 'ol': return '\n' + children + '\n';
case 'li': return '- ' + children + '\n';
case 'blockquote': return '\n> ' + children + '\n';
case 'div': return children;
case 'span': return children;
default: return children;
}
}
let markdown = convertNode(temp);
markdown = markdown.replace(/\n{3,}/g, '\n\n');
markdown = markdown.replace(/ /g, ' ');
markdown = markdown.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
return markdown.trim();
}
// ============================================
// 获取消息内容
// ============================================
// 使用元素在所有消息中的稳定索引作为 ID
function getMessageId(element) {
const allMessages = document.querySelectorAll(SELECTORS.AI_RESPONSE + ', ' + SELECTORS.USER_MESSAGE);
for (let i = 0; i < allMessages.length; i++) {
if (allMessages[i] === element) {
return `gemini-msg-${i}`;
}
}
// 兜底:使用元素在父元素中的位置
const position = Array.from(element.parentElement?.children || []).indexOf(element);
return `gemini-msg-${position}`;
}
function getMessageContent(element) {
const contentEl = element.querySelector(SELECTORS.MESSAGE_CONTENT);
if (contentEl) {
// 复制内容并移除thinking和导出按钮
const clone = contentEl.cloneNode(true);
clone.querySelectorAll(SELECTORS.THINKING).forEach(el => el.remove());
clone.querySelectorAll('.gemini-export-buttons').forEach(el => el.remove());
return clone.innerHTML;
}
// 兜底
const clone = element.cloneNode(true);
clone.querySelectorAll('.gemini-export-buttons').forEach(el => el.remove());
return clone.innerHTML;
}
function getMessageText(element) {
const contentEl = element.querySelector(SELECTORS.MESSAGE_CONTENT);
return contentEl ? (contentEl.innerText || contentEl.textContent) : (element.innerText || element.textContent);
}
function getThinkingContent(element) {
const thinkingEl = element.querySelector(SELECTORS.THINKING);
return thinkingEl ? thinkingEl.innerHTML : null;
}
function isAiMessage(element) {
return element.matches(SELECTORS.AI_RESPONSE) || element.closest(SELECTORS.AI_RESPONSE);
}
// ============================================
// 文件下载
// ============================================
function downloadFile(content, filename, mimeType) {
try {
const blob = new Blob([content], { type: mimeType + ';charset=utf-8' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
log('导出成功: ' + filename);
return true;
} catch (err) {
warn('下载失败: ' + err.message);
return false;
}
}
// ============================================
// 单条导出函数
// ============================================
function exportAsMarkdown(element) {
const content = getMessageContent(element);
const isUser = !isAiMessage(element);
const timestamp = Date.now();
const mdContent = htmlToMarkdown(content);
let md = isUser ? '# 用户问题\n\n' + mdContent + '\n\n' : '# AI 回答\n\n' + mdContent + '\n\n';
downloadFile(md, `gemini-${timestamp}.md`, 'text/markdown');
}
function exportAsTXT(element) {
const text = getMessageText(element);
const isUser = !isAiMessage(element);
const timestamp = Date.now();
let txt = isUser ? '用户问题\n========\n\n' + text + '\n\n' : 'AI 回答\n========\n\n' + text + '\n\n';
downloadFile(txt, `gemini-${timestamp}.txt`, 'text/plain');
}
function exportAsHtml(element) {
const content = getMessageContent(element);
const isUser = !isAiMessage(element);
const timestamp = Date.now();
const html = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gemini 对话导出</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; max-width: 800px; margin: 0 auto; padding: 40px 20px; line-height: 1.8; color: #333; }
h1, h2 { color: #1a1a1a; margin-top: 1.5em; }
pre { background: #f6f8fa; padding: 16px; border-radius: 6px; overflow-x: auto; }
code { background: #f6f8fa; padding: 2px 4px; border-radius: 3px; }
pre code { background: none; padding: 0; }
blockquote { border-left: 4px solid #dfe2e5; color: #6a737d; margin: 0; padding-left: 16px; }
.timestamp { color: #999; font-size: 12px; margin-bottom: 20px; }
</style>
</head>
<body>
<div class="timestamp">导出时间: ${new Date().toLocaleString('zh-CN')}</div>
<h2>${isUser ? '用户问题' : 'AI 回答'}</h2>
<div>${content}</div>
</body>
</html>`;
downloadFile(html, `gemini-${timestamp}.html`, 'text/html');
}
// ============================================
// 批量导出函数
// ============================================
// 从DOM实时获取消息内容(避免使用缓存)
function getRealtimeContent(element) {
// 优先获取实际的回答内容,排除thinking和导出按钮部分
const contentEl = element.querySelector(SELECTORS.MESSAGE_CONTENT);
if (contentEl) {
// 复制内容并移除thinking和导出按钮
const clone = contentEl.cloneNode(true);
clone.querySelectorAll(SELECTORS.THINKING).forEach(el => el.remove());
clone.querySelectorAll('.gemini-export-buttons').forEach(el => el.remove());
return clone.innerHTML;
}
// 兜底:返回元素内容但移除导出按钮
const clone = element.cloneNode(true);
clone.querySelectorAll('.gemini-export-buttons').forEach(el => el.remove());
return clone.innerHTML;
}
function exportBatchMarkdown() {
if (selectedMessages.size === 0) {
alert('请先选择要导出的消息');
return;
}
let md = '# Gemini 对话导出\n\n';
md += `导出时间: ${new Date().toLocaleString('zh-CN')}\n\n`;
md += '---\n\n';
// 直接遍历DOM,按页面显示顺序导出选中的消息
const allMessages = document.querySelectorAll(SELECTORS.AI_RESPONSE + ', ' + SELECTORS.USER_MESSAGE);
allMessages.forEach(msg => {
if (!isTopLevelMessage(msg)) return;
const msgId = getMessageId(msg);
if (!selectedMessages.has(msgId)) return;
// 实时获取内容
const content = getRealtimeContent(msg);
const mdContent = htmlToMarkdown(content);
const isUser = !isAiMessage(msg);
if (isUser) {
md += '## 用户问题\n\n' + mdContent + '\n\n';
} else {
md += '## AI 回答\n\n' + mdContent + '\n\n';
}
md += '---\n\n';
});
downloadFile(md, `gemini-batch-${Date.now()}.md`, 'text/markdown');
exitSelectionMode();
}
function exportBatchTXT() {
if (selectedMessages.size === 0) {
alert('请先选择要导出的消息');
return;
}
let txt = 'Gemini 对话导出\n' + '================\n\n';
txt += `导出时间: ${new Date().toLocaleString('zh-CN')}\n\n`;
txt += '================\n\n';
// 直接遍历DOM,按页面显示顺序导出选中的消息
const allMessages = document.querySelectorAll(SELECTORS.AI_RESPONSE + ', ' + SELECTORS.USER_MESSAGE);
allMessages.forEach(msg => {
if (!isTopLevelMessage(msg)) return;
const msgId = getMessageId(msg);
if (!selectedMessages.has(msgId)) return;
// 实时获取文本内容
const content = getRealtimeContent(msg);
const temp = document.createElement('div');
temp.innerHTML = content;
const textContent = temp.innerText || temp.textContent;
const isUser = !isAiMessage(msg);
if (isUser) {
txt += '【用户问题】\n' + textContent + '\n\n';
} else {
txt += '【AI 回答】\n' + textContent + '\n\n';
}
txt += '----------------\n\n';
});
downloadFile(txt, `gemini-batch-${Date.now()}.txt`, 'text/plain');
exitSelectionMode();
}
function exportBatchHTML() {
if (selectedMessages.size === 0) {
alert('请先选择要导出的消息');
return;
}
let html = `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gemini 对话导出</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; max-width: 800px; margin: 0 auto; padding: 40px 20px; line-height: 1.8; color: #333; }
h1 { border-bottom: 2px solid #eee; padding-bottom: 10px; }
h2 { color: #1a1a1a; margin-top: 2em; border-bottom: 1px solid #eee; padding-bottom: 8px; }
pre { background: #f6f8fa; padding: 16px; border-radius: 6px; overflow-x: auto; }
code { background: #f6f8fa; padding: 2px 4px; border-radius: 3px; }
pre code { background: none; padding: 0; }
.message { margin-bottom: 2em; padding-bottom: 1em; border-bottom: 1px dashed #ddd; }
.timestamp { color: #999; font-size: 12px; margin-bottom: 30px; }
.separator { border: none; border-top: 1px dashed #ddd; margin: 2em 0; }
</style>
</head>
<body>
<h1>Gemini 对话导出</h1>
<div class="timestamp">导出时间: ${new Date().toLocaleString('zh-CN')}</div>`;
// 直接遍历DOM,按页面显示顺序导出选中的消息
const allMessages = document.querySelectorAll(SELECTORS.AI_RESPONSE + ', ' + SELECTORS.USER_MESSAGE);
allMessages.forEach(msg => {
if (!isTopLevelMessage(msg)) return;
const msgId = getMessageId(msg);
if (!selectedMessages.has(msgId)) return;
// 实时获取内容
const content = getRealtimeContent(msg);
const isUser = !isAiMessage(msg);
html += '<div class="message">';
if (isUser) {
html += '<h2>用户问题</h2>';
} else {
html += '<h2>AI 回答</h2>';
}
html += '<div>' + content + '</div>';
html += '</div><hr class="separator">';
});
html += '</body></html>';
downloadFile(html, `gemini-batch-${Date.now()}.html`, 'text/html');
exitSelectionMode();
}
// ============================================
// 选择模式
// ============================================
function enterSelectionMode() {
isSelectionMode = true;
document.body.classList.add('gemini-selection-mode');
// 显示选择工具栏
showSelectionToolbar();
// 为所有消息添加复选框
addCheckboxesToMessages();
}
function exitSelectionMode() {
isSelectionMode = false;
selectedMessages.clear();
document.body.classList.remove('gemini-selection-mode');
// 隐藏工具栏
hideSelectionToolbar();
// 移除复选框
removeCheckboxesFromMessages();
}
// 检查元素是否是顶级的消息容器(不包含在另一个消息容器中)
function isTopLevelMessage(element) {
const parent = element.parentElement;
if (!parent) return true;
// 检查父元素是否也是消息容器
const parentSelector = SELECTORS.AI_RESPONSE + ', ' + SELECTORS.USER_MESSAGE;
return !parent.matches(parentSelector);
}
function addCheckboxesToMessages() {
// 获取所有匹配的消息元素
const allElements = document.querySelectorAll(SELECTORS.AI_RESPONSE + ', ' + SELECTORS.USER_MESSAGE);
allElements.forEach(msg => {
// 只处理顶级元素,过滤掉嵌套的
if (!isTopLevelMessage(msg)) return;
// 使用 data 属性标记已添加复选框的消息
if (msg.dataset.geminiHasCheckbox) return;
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.className = 'gemini-msg-checkbox';
checkbox.style.cssText = 'position:absolute;top:8px;left:8px;width:18px;height:18px;cursor:pointer;z-index:100;';
checkbox.addEventListener('change', (e) => {
e.stopPropagation();
const msgId = getMessageId(msg);
if (checkbox.checked) {
selectedMessages.add(msgId);
msg.classList.add('gemini-message-selected');
} else {
selectedMessages.delete(msgId);
msg.classList.remove('gemini-message-selected');
}
updateSelectionCount();
});
msg.style.position = 'relative';
msg.dataset.geminiHasCheckbox = 'true'; // 标记已添加
msg.insertBefore(checkbox, msg.firstChild);
});
}
function removeCheckboxesFromMessages() {
document.querySelectorAll('.gemini-msg-checkbox').forEach(cb => cb.remove());
document.querySelectorAll('.gemini-message-selected').forEach(msg => {
msg.classList.remove('gemini-message-selected');
});
// 清除标记
document.querySelectorAll('[data-gemini-has-checkbox]').forEach(msg => {
delete msg.dataset.geminiHasCheckbox;
});
}
function selectAll() {
const messages = document.querySelectorAll(SELECTORS.AI_RESPONSE + ', ' + SELECTORS.USER_MESSAGE);
messages.forEach(msg => {
if (!isTopLevelMessage(msg)) return;
const checkbox = msg.querySelector('.gemini-msg-checkbox');
if (checkbox) {
checkbox.checked = true;
const msgId = getMessageId(msg);
selectedMessages.add(msgId);
msg.classList.add('gemini-message-selected');
}
});
updateSelectionCount();
}
function deselectAll() {
selectedMessages.clear();
document.querySelectorAll('.gemini-msg-checkbox').forEach(cb => cb.checked = false);
document.querySelectorAll('.gemini-message-selected').forEach(msg => {
msg.classList.remove('gemini-message-selected');
});
updateSelectionCount();
}
function selectAllAi() {
const messages = document.querySelectorAll(SELECTORS.AI_RESPONSE);
messages.forEach(msg => {
if (!isTopLevelMessage(msg)) return;
const checkbox = msg.querySelector('.gemini-msg-checkbox');
if (checkbox) {
checkbox.checked = true;
const msgId = getMessageId(msg);
selectedMessages.add(msgId);
msg.classList.add('gemini-message-selected');
}
});
updateSelectionCount();
}
// ============================================
// 选择工具栏 UI
// ============================================
function showSelectionToolbar() {
let toolbar = document.querySelector('.gemini-selection-toolbar');
if (!toolbar) {
toolbar = document.createElement('div');
toolbar.className = 'gemini-selection-toolbar';
toolbar.innerHTML = `
<div class="toolbar-content">
<button class="toolbar-btn" id="gemini-select-all">全选</button>
<button class="toolbar-btn" id="gemini-select-ai">全选AI</button>
<button class="toolbar-btn" id="gemini-deselect-all">取消全选</button>
<span class="toolbar-count">已选择 <span id="gemini-selected-count">0</span> 条</span>
<button class="toolbar-btn primary" id="gemini-open-export">批量导出</button>
<button class="toolbar-btn close" id="gemini-exit-selection">退出选择</button>
</div>
`;
document.body.appendChild(toolbar);
// 绑定事件
toolbar.querySelector('#gemini-select-all').onclick = selectAll;
toolbar.querySelector('#gemini-select-ai').onclick = selectAllAi;
toolbar.querySelector('#gemini-deselect-all').onclick = deselectAll;
toolbar.querySelector('#gemini-open-export').onclick = showBatchExportDialog;
toolbar.querySelector('#gemini-exit-selection').onclick = exitSelectionMode;
}
toolbar.style.display = 'flex';
}
function hideSelectionToolbar() {
const toolbar = document.querySelector('.gemini-selection-toolbar');
if (toolbar) toolbar.style.display = 'none';
}
function updateSelectionCount() {
const countEl = document.getElementById('gemini-selected-count');
if (countEl) countEl.textContent = selectedMessages.size;
}
// ============================================
// 批量导出对话框
// ============================================
function showBatchExportDialog() {
if (selectedMessages.size === 0) {
alert('请先选择要导出的消息');
return;
}
let dialog = document.querySelector('.gemini-batch-dialog');
if (!dialog) {
dialog = document.createElement('div');
dialog.className = 'gemini-batch-dialog';
dialog.innerHTML = `
<div class="dialog-overlay"></div>
<div class="dialog-content">
<div class="dialog-header">
<h2>批量导出</h2>
<button class="dialog-close">×</button>
</div>
<div class="dialog-body">
<p>已选择 <strong id="gemini-dialog-count">0</strong> 条消息</p>
<div class="format-options">
<button class="format-btn" data-format="markdown">
<span>M</span>
<span>Markdown</span>
</button>
<button class="format-btn" data-format="txt">
<span>T</span>
<span>TXT</span>
</button>
<button class="format-btn" data-format="html">
<span>H</span>
<span>HTML</span>
</button>
</div>
</div>
<div class="dialog-footer">
<button class="cancel-btn">取消</button>
</div>
</div>
`;
document.body.appendChild(dialog);
// 绑定事件
dialog.querySelector('.dialog-close').onclick = () => closeBatchExportDialog();
dialog.querySelector('.cancel-btn').onclick = () => closeBatchExportDialog();
dialog.querySelector('.dialog-overlay').onclick = () => closeBatchExportDialog();
dialog.querySelectorAll('.format-btn').forEach(btn => {
btn.onclick = () => {
const format = btn.dataset.format;
switch(format) {
case 'markdown': exportBatchMarkdown(); break;
case 'txt': exportBatchTXT(); break;
case 'html': exportBatchHTML(); break;
}
closeBatchExportDialog();
};
});
}
// 显示对话框前更新消息数
const dialogCountEl = dialog.querySelector('#gemini-dialog-count');
if (dialogCountEl) dialogCountEl.textContent = selectedMessages.size;
dialog.style.display = 'flex';
}
function closeBatchExportDialog() {
const dialog = document.querySelector('.gemini-batch-dialog');
if (dialog) dialog.style.display = 'none';
}
// ============================================
// FAB Hub 浮动按钮
// ============================================
function createFabHub() {
if (document.querySelector('.gemini-fab-hub')) return;
const fab = document.createElement('div');
fab.className = 'gemini-fab-hub';
fab.innerHTML = `
<button class="fab-main" title="Chat2File-Gemini">
<svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8l-6-6zm4 18H6V4h7v5h5v11z"/>
</svg>
</button>
<div class="fab-menu">
<button class="fab-item" id="fab-batch-export" title="批量导出">
<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor">
<path d="M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z"/>
</svg>
<span>批量导出</span>
</button>
</div>
`;
document.body.appendChild(fab);
// 主按钮点击展开/收起菜单
const mainBtn = fab.querySelector('.fab-main');
mainBtn.onclick = (e) => {
e.stopPropagation();
fab.classList.toggle('open');
};
// 点击其他地方关闭菜单
document.addEventListener('click', (e) => {
if (!fab.contains(e.target)) fab.classList.remove('open');
});
// 批量导出按钮
fab.querySelector('#fab-batch-export').onclick = () => {
fab.classList.remove('open');
enterSelectionMode();
};
}
// ============================================
// 单条导出按钮
// ============================================
function addExportButtons(messageElement) {
// 使用 data 属性标记已添加导出按钮的消息
if (messageElement.dataset.geminiHasExportButtons) return;
const actionBar = messageElement.querySelector(SELECTORS.ACTION_BAR);
if (!actionBar) return;
const container = document.createElement('div');
container.className = 'gemini-export-buttons';
container.style.cssText = 'display:flex;gap:6px;margin-top:10px;padding-top:10px;flex-wrap:wrap;align-items:center;';
const buttons = [
{ label: 'Markdown', fn: () => exportAsMarkdown(messageElement), color: '#0369a1' },
{ label: 'TXT', fn: () => exportAsTXT(messageElement), color: '#059669' },
{ label: 'HTML', fn: () => exportAsHtml(messageElement), color: '#7c3aed' }
];
buttons.forEach(btn => {
const button = document.createElement('button');
button.textContent = btn.label;
button.style.cssText = `padding:5px 10px;font-size:11px;border:1px solid #ddd;border-radius:4px;background:white;color:${btn.color};cursor:pointer;font-weight:500;white-space:nowrap;`;
button.onmouseenter = () => button.style.background = '#f3f4f6';
button.onmouseleave = () => button.style.background = 'white';
button.onclick = (e) => { e.stopPropagation(); btn.fn(); };
container.appendChild(button);
});
actionBar.parentNode.insertBefore(container, actionBar.nextSibling);
messageElement.dataset.geminiHasExportButtons = 'true'; // 标记已添加
}
// ============================================
// 消息处理
// ============================================
function processMessages() {
const messages = document.querySelectorAll(SELECTORS.AI_RESPONSE + ', ' + SELECTORS.USER_MESSAGE);
messages.forEach(msg => {
// 只处理顶级消息元素
if (!isTopLevelMessage(msg)) return;
const msgId = getMessageId(msg);
if (!exportedMessages.has(msgId)) {
exportedMessages.set(msgId, {
element: msg,
type: isAiMessage(msg) ? 'ai' : 'user',
content: getMessageContent(msg),
thinking: getThinkingContent(msg),
exported: false
});
}
addExportButtons(msg);
});
}
// ============================================
// 初始化
// ============================================
function init() {
log('初始化中...');
// 创建 FAB Hub
createFabHub();
// 处理现有消息
processMessages();
// 监听新消息
const observer = new MutationObserver(() => {
processMessages();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
log('初始化完成');
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => setTimeout(init, 1500));
} else {
setTimeout(init, 1500);
}
// 添加样式
const style = document.createElement('style');
style.textContent = `
/* FAB Hub */
.gemini-fab-hub {
position: fixed;
right: 24px;
bottom: 24px;
z-index: 9999;
display: flex;
flex-direction: column;
align-items: center;
}
.fab-main {
width: 48px;
height: 48px;
border-radius: 12px;
background: linear-gradient(135deg, #6366f1, #8b5cf6);
border: none;
color: white;
cursor: pointer;
box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
display: flex;
align-items: center;
justify-content: center;
transition: transform 0.2s, box-shadow 0.2s;
}
.fab-main:hover {
transform: translateY(-2px);
box-shadow: 0 6px 16px rgba(99, 102, 241, 0.5);
}
.fab-menu {
position: absolute;
bottom: 60px;
display: flex;
flex-direction: column;
gap: 8px;
opacity: 0;
transform: translateY(10px);
pointer-events: none;
transition: opacity 0.2s, transform 0.2s;
}
.gemini-fab-hub.open .fab-menu {
opacity: 1;
transform: translateY(0);
pointer-events: auto;
}
.fab-item {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 16px;
background: white;
border: 1px solid #e5e7eb;
border-radius: 8px;
cursor: pointer;
font-size: 13px;
color: #374151;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
transition: background 0.2s;
}
.fab-item:hover {
background: #f3f4f6;
}
/* 选择模式 */
.gemini-selection-mode .gemini-export-buttons {
display: none !important;
}
.gemini-message-selected {
outline: 2px solid #6366f1 !important;
outline-offset: 2px;
background: rgba(99, 102, 241, 0.05) !important;
}
/* 选择工具栏 */
.gemini-selection-toolbar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
background: white;
border-top: 1px solid #e5e7eb;
padding: 12px 20px;
display: none;
justify-content: center;
align-items: center;
gap: 12px;
box-shadow: 0 -4px 12px rgba(0,0,0,0.1);
z-index: 10000;
}
.toolbar-content {
display: flex;
align-items: center;
gap: 10px;
}
.toolbar-btn {
padding: 8px 14px;
border: 1px solid #e5e7eb;
border-radius: 6px;
background: white;
cursor: pointer;
font-size: 13px;
transition: background 0.2s;
}
.toolbar-btn:hover { background: #f3f4f6; }
.toolbar-btn.primary {
background: #6366f1;
color: white;
border-color: #6366f1;
}
.toolbar-btn.primary:hover { background: #4f46e5; }
.toolbar-btn.close {
color: #ef4444;
}
.toolbar-count {
font-size: 13px;
color: #6b7280;
margin: 0 8px;
}
/* 批量导出对话框 */
.gemini-batch-dialog {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.5);
display: none;
justify-content: center;
align-items: center;
z-index: 10001;
}
.dialog-content {
background: white;
border-radius: 16px;
max-width: 400px;
width: 90%;
overflow: hidden;
box-shadow: 0 20px 40px rgba(0,0,0,0.2);
}
.dialog-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 20px;
border-bottom: 1px solid #e5e7eb;
}
.dialog-header h2 { margin: 0; font-size: 18px; }
.dialog-close {
background: none;
border: none;
font-size: 24px;
cursor: pointer;
color: #9ca3af;
}
.dialog-body { padding: 24px 20px; text-align: center; }
.dialog-body p { margin: 0 0 20px; color: #6b7280; }
.format-options {
display: flex;
gap: 12px;
justify-content: center;
}
.format-btn {
display: flex;
flex-direction: column;
align-items: center;
padding: 16px 20px;
border: 2px solid #e5e7eb;
border-radius: 12px;
background: white;
cursor: pointer;
transition: all 0.2s;
}
.format-btn:hover {
border-color: #6366f1;
background: #f5f3ff;
}
.format-btn span:first-child {
font-size: 20px;
font-weight: bold;
color: #6366f1;
}
.format-btn span:last-child {
font-size: 12px;
margin-top: 4px;
color: #374151;
}
.dialog-footer {
padding: 16px 20px;
border-top: 1px solid #e5e7eb;
text-align: center;
}
.cancel-btn {
padding: 10px 32px;
border: 1px solid #e5e7eb;
border-radius: 8px;
background: white;
cursor: pointer;
font-size: 14px;
}
.cancel-btn:hover { background: #f3f4f6; }
/* 暗色模式 */
@media (prefers-color-scheme: dark) {
.gemini-selection-toolbar { background: #1f2937; border-color: #374151; }
.fab-item { background: #374151; border-color: #4b5563; color: #f9fafb; }
.fab-item:hover { background: #4b5563; }
.toolbar-btn { background: #374151; border-color: #4b5563; color: #f9fafb; }
.toolbar-btn:hover { background: #4b5563; }
.dialog-content { background: #1f2937; }
.dialog-header { border-color: #374151; }
.dialog-header h2 { color: #f9fafb; }
.dialog-close { color: #9ca3af; }
.dialog-body p { color: #9ca3af; }
.format-btn { border-color: #374151; background: #111827; }
.format-btn span:last-child { color: #d1d5db; }
.dialog-footer { border-color: #374151; }
.cancel-btn { background: #374151; border-color: #4b5563; color: #f9fafb; }
}
`;
document.head.appendChild(style);
})();