forked from yym68686/Cerebr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
866 lines (752 loc) · 28.4 KB
/
content.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
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
// console.log('Cerebr content script loaded at:', new Date().toISOString());
// console.log('Window location:', window.location.href);
// console.log('Document readyState:', document.readyState);
class CerebrSidebar {
constructor() {
this.isVisible = false;
this.sidebarWidth = 430;
this.initialized = false;
this.pageKey = window.location.origin + window.location.pathname;
this.lastUrl = window.location.href;
// console.log('CerebrSidebar 实例创建');
// this.lastToggleTime = null; // 添加上次执行时间存储
this.initializeSidebar();
this.setupUrlChangeListener();
this.setupDragAndDrop(); // 添加拖放事件监听器
}
setupUrlChangeListener() {
let lastUrl = window.location.href;
// 检查URL是否发生实质性变化
const hasUrlChanged = (currentUrl) => {
if (currentUrl === lastUrl) return false;
if (document.contentType === 'application/pdf') return false;
const oldUrl = new URL(lastUrl);
const newUrl = new URL(currentUrl);
return oldUrl.pathname !== newUrl.pathname || oldUrl.search !== newUrl.search;
};
// 处理URL变化
const handleUrlChange = () => {
const currentUrl = window.location.href;
if (hasUrlChanged(currentUrl)) {
console.log('URL变化:', '从:', lastUrl, '到:', currentUrl);
lastUrl = currentUrl;
// 获取iframe并发送消息
const iframe = this.sidebar?.querySelector('.cerebr-sidebar__iframe');
if (iframe) {
console.log('发送URL变化消息到iframe');
iframe.contentWindow.postMessage({
type: 'URL_CHANGED',
url: currentUrl
}, '*');
}
}
};
// 监听popstate事件
window.addEventListener('popstate', () => {
console.log('popstate事件触发');
handleUrlChange();
});
// 重写history方法
const originalPushState = history.pushState;
const originalReplaceState = history.replaceState;
history.pushState = function() {
originalPushState.apply(this, arguments);
console.log('pushState被调用');
handleUrlChange();
};
history.replaceState = function() {
originalReplaceState.apply(this, arguments);
console.log('replaceState被调用');
handleUrlChange();
};
// 添加定期检查
setInterval(handleUrlChange, 1000);
}
async saveState() {
try {
const states = await chrome.storage.local.get('sidebarStates') || { sidebarStates: {} };
if (!states.sidebarStates) {
states.sidebarStates = {};
}
states.sidebarStates[this.pageKey] = {
isVisible: this.isVisible,
width: this.sidebarWidth
};
await chrome.storage.local.set(states);
} catch (error) {
console.error('保存侧边栏状态失败:', error);
}
}
async loadState() {
try {
const states = await chrome.storage.local.get('sidebarStates');
if (states.sidebarStates && states.sidebarStates[this.pageKey]) {
const state = states.sidebarStates[this.pageKey];
this.isVisible = state.isVisible;
this.sidebarWidth = state.width;
if (this.isVisible) {
this.sidebar.classList.add('visible');
}
this.sidebar.style.width = `${this.sidebarWidth}px`;
}
} catch (error) {
console.error('加载侧边栏状态失败:', error);
}
}
async initializeSidebar() {
try {
// console.log('开始初始化侧边栏');
const container = document.createElement('cerebr-root');
// 防止外部JavaScript访问和修改我们的元素
Object.defineProperty(container, 'remove', {
configurable: false,
writable: false,
value: () => {
console.log('阻止移除侧边栏');
return false;
}
});
// 使用closed模式的shadowRoot以增加隔离性
const shadow = container.attachShadow({ mode: 'closed' });
const style = document.createElement('style');
style.textContent = `
:host {
all: initial;
contain: style layout size;
}
.cerebr-sidebar {
position: fixed;
top: 20px;
right: -450px;
width: 430px;
height: calc(100vh - 40px);
background: var(--cerebr-bg-color, #ffffff);
color: var(--cerebr-text-color, #000000);
box-shadow: -2px 0 15px rgba(0,0,0,0.1);
z-index: 2147483647;
border-radius: 12px;
margin-right: 20px;
overflow: hidden;
visibility: hidden;
transform: translateX(0);
pointer-events: none;
contain: style layout size;
isolation: isolate;
}
.cerebr-sidebar.initialized {
visibility: visible;
transition: transform 0.3s ease;
pointer-events: auto;
}
@media (prefers-color-scheme: dark) {
.cerebr-sidebar {
--cerebr-bg-color: #282c34;
--cerebr-text-color: #abb2bf;
box-shadow: -2px 0 20px rgba(0,0,0,0.3);
}
}
.cerebr-sidebar.visible {
transform: translateX(-450px);
}
.cerebr-sidebar__content {
height: 100%;
overflow: hidden;
border-radius: 12px;
contain: style layout size;
}
.cerebr-sidebar__iframe {
width: 100%;
height: 100%;
border: none;
background: var(--cerebr-bg-color, #ffffff);
contain: strict;
}
`;
this.sidebar = document.createElement('div');
this.sidebar.className = 'cerebr-sidebar';
// 防止外部JavaScript访问和修改侧边栏
Object.defineProperty(this.sidebar, 'remove', {
configurable: false,
writable: false,
value: () => {
console.log('阻止移除侧边栏');
return false;
}
});
const header = document.createElement('div');
header.className = 'cerebr-sidebar__header';
const resizer = document.createElement('div');
resizer.className = 'cerebr-sidebar__resizer';
const content = document.createElement('div');
content.className = 'cerebr-sidebar__content';
const iframe = document.createElement('iframe');
iframe.className = 'cerebr-sidebar__iframe';
iframe.src = chrome.runtime.getURL('index.html');
iframe.allow = 'clipboard-write';
content.appendChild(iframe);
this.sidebar.appendChild(header);
this.sidebar.appendChild(resizer);
this.sidebar.appendChild(content);
shadow.appendChild(style);
shadow.appendChild(this.sidebar);
// 先加载状态
await this.loadState();
// 添加到文档并保护它
const root = document.documentElement;
root.appendChild(container);
// 使用MutationObserver确保我们的元素不会被移除
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.type === 'childList') {
const removedNodes = Array.from(mutation.removedNodes);
if (removedNodes.includes(container)) {
console.log('检测到侧边栏被移除,正在恢复...');
root.appendChild(container);
}
}
}
});
observer.observe(root, {
childList: true
});
// console.log('侧边栏已添加到文档');
this.setupEventListeners(resizer);
// 使用 requestAnimationFrame 确保状态已经应用
requestAnimationFrame(() => {
this.sidebar.classList.add('initialized');
this.initialized = true;
// console.log('侧边栏初始化完成');
});
} catch (error) {
console.error('初始化侧边栏失败:', error);
}
}
setupEventListeners(resizer) {
let startX, startWidth;
resizer.addEventListener('mousedown', (e) => {
startX = e.clientX;
startWidth = this.sidebarWidth;
const handleMouseMove = (e) => {
const diff = startX - e.clientX;
this.sidebarWidth = Math.min(Math.max(300, startWidth + diff), 800);
this.sidebar.style.width = `${this.sidebarWidth}px`;
};
const handleMouseUp = () => {
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
};
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
});
}
toggle() {
if (!this.initialized) return;
try {
// 在改变可见性之前保存旧状态
const wasVisible = this.isVisible;
this.isVisible = !this.isVisible;
// 更新DOM状态
if (this.isVisible) {
this.sidebar.classList.add('visible');
} else {
this.sidebar.classList.remove('visible');
}
// 保存状态
this.saveState();
// 如果从不可见变为可见,通知iframe并聚焦输入框
if (!wasVisible && this.isVisible) {
const iframe = this.sidebar.querySelector('.cerebr-sidebar__iframe');
if (iframe) {
iframe.contentWindow.postMessage({ type: 'FOCUS_INPUT' }, '*');
}
}
} catch (error) {
console.error('切换侧边栏失败:', error);
}
}
setupDragAndDrop() {
// console.log('初始化拖放功能');
// 存储最后一次设置的图片数据
let lastImageData = null;
// 检查是否在侧边栏范围内的函数
const isInSidebarBounds = (x, y) => {
if (!this.sidebar) return false;
const sidebarRect = this.sidebar.getBoundingClientRect();
return (
x >= sidebarRect.left &&
x <= sidebarRect.right &&
y >= sidebarRect.top &&
y <= sidebarRect.bottom
);
};
// 监听页面上的所有图片
document.addEventListener('dragstart', (e) => {
console.log('拖动开始,目标元素:', e.target.tagName);
const img = e.target;
if (img.tagName === 'IMG') {
console.log('检测到图片拖动,图片src:', img.src);
// 尝试直接获取图片的 src
try {
// 对于跨域图片,尝试使用 fetch 获取
console.log('尝试获取图片数据');
fetch(img.src)
.then(response => response.blob())
.then(blob => {
console.log('成功获取图片blob数据,大小:', blob.size);
const reader = new FileReader();
reader.onloadend = () => {
const base64Data = reader.result;
console.log('成功转换为base64数据');
const imageData = {
type: 'image',
data: base64Data,
name: img.alt || '拖放图片'
};
console.log('设置拖动数据:', imageData.name);
lastImageData = imageData; // 保存最后一次的图片数据
// 使用自定义事件类型,避免数据丢失
e.dataTransfer.setData('application/x-cerebr-image', JSON.stringify(imageData));
e.dataTransfer.effectAllowed = 'copy';
};
reader.readAsDataURL(blob);
})
.catch(error => {
console.error('获取图片数据失败:', error);
// 如果 fetch 失败,回退到 canvas 方法
console.log('尝试使用Canvas方法获取图片数据');
try {
const canvas = document.createElement('canvas');
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const base64Data = canvas.toDataURL(img.src.match(/\.png$/i) ? 'image/png' : 'image/jpeg');
console.log('成功使用Canvas获取图片数据');
const imageData = {
type: 'image',
data: base64Data,
name: img.alt || '拖放图片'
};
console.log('设置拖动数据:', imageData.name);
lastImageData = imageData; // 保存最后一次的图片数据
// 使用自定义事件类型,避免数据丢失
e.dataTransfer.setData('application/x-cerebr-image', JSON.stringify(imageData));
e.dataTransfer.effectAllowed = 'copy';
} catch (canvasError) {
console.error('Canvas获取图片数据失败:', canvasError);
}
});
} catch (error) {
console.error('处理图片拖动失败:', error);
}
}
});
// 监听拖动结束事件
document.addEventListener('dragend', (e) => {
const inSidebar = isInSidebarBounds(e.clientX, e.clientY);
console.log('拖动结束,是否在侧边栏内:', inSidebar, '坐标:', e.clientX, e.clientY);
const iframe = this.sidebar?.querySelector('.cerebr-sidebar__iframe');
if (iframe && inSidebar && lastImageData && this.isVisible) { // 确保侧边栏可见
console.log('在侧边栏内放下,发送图片数据到iframe');
iframe.contentWindow.postMessage({
type: 'DROP_IMAGE',
imageData: lastImageData
}, '*');
}
// 重置状态
lastImageData = null;
});
}
}
let sidebar;
try {
sidebar = new CerebrSidebar();
// console.log('侧边栏实例已创建');
} catch (error) {
console.error('创建侧边栏实例失败:', error);
}
// 修改消息监听器
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type != 'REQUEST_STARTED' && message.type != 'REQUEST_COMPLETED' &&
message.type != 'REQUEST_FAILED' && message.type != 'PING') {
// console.log('content.js 收到消息:', message.type);
}
// 处理 PING 消息
if (message.type === 'PING') {
sendResponse({
type: 'PONG',
timestamp: message.timestamp,
responseTime: Date.now()
});
return true;
}
// 处理侧边栏切换命令
if (message.type === 'TOGGLE_SIDEBAR_onClicked' || message.type === 'TOGGLE_SIDEBAR_toggle_sidebar') {
try {
if (sidebar) {
sidebar.toggle();
sendResponse({ success: true, status: sidebar.isVisible });
} else {
console.error('侧边栏实例不存在');
sendResponse({ success: false, error: 'Sidebar instance not found' });
}
} catch (error) {
console.error('处理切换命令失败:', error);
sendResponse({ success: false, error: error.message });
}
return true;
}
// 处理清空聊天记录命令
if (message.type === 'CLEAR_CHAT') {
try {
const iframe = sidebar?.sidebar?.querySelector('.cerebr-sidebar__iframe');
if (iframe) {
iframe.contentWindow.postMessage({ type: 'CLEAR_CHAT_COMMAND' }, '*');
sendResponse({ success: true });
} else {
console.error('找不到侧边栏iframe');
sendResponse({ success: false, error: 'Iframe not found' });
}
} catch (error) {
console.error('处理清空聊天命令失败:', error);
sendResponse({ success: false, error: error.message });
}
return true;
}
// 处理获取页面内容请求
if (message.type === 'GET_PAGE_CONTENT_INTERNAL') {
// console.log('收到获取页面内容请求');
isProcessing = true;
extractPageContent().then(content => {
isProcessing = false;
sendResponse(content);
}).catch(error => {
console.error('提取页面内容失败:', error);
isProcessing = false;
sendResponse(null);
});
return true;
}
// 处理 NEW_CHAT 消息
if (message.type === 'NEW_CHAT') {
const iframe = sidebar?.sidebar?.querySelector('.cerebr-sidebar__iframe');
if (iframe) {
iframe.contentWindow.postMessage({ type: 'NEW_CHAT' }, '*');
}
sendResponse({ success: true });
return true;
}
return true;
});
const port = chrome.runtime.connect({ name: 'cerebr-sidebar' });
port.onDisconnect.addListener(() => {
console.log('与 background 的连接已断开');
});
function sendInitMessage(retryCount = 0) {
const maxRetries = 10;
const retryDelay = 1000;
// console.log(`尝试发送初始化消息,第 ${retryCount + 1} 次尝试`);
chrome.runtime.sendMessage({
type: 'CONTENT_LOADED',
url: window.location.href
}).then(response => {
// console.log('Background 响应:', response);
}).catch(error => {
console.log('发送消息失败:', error);
if (retryCount < maxRetries) {
console.log(`${retryDelay}ms 后重试...`);
setTimeout(() => sendInitMessage(retryCount + 1), retryDelay);
} else {
console.error('达最大重试次数,初始化消息发送失败');
}
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
setTimeout(sendInitMessage, 500);
});
} else {
setTimeout(sendInitMessage, 500);
}
window.addEventListener('error', (event) => {
console.error('全局错误:', event.error);
});
window.addEventListener('unhandledrejection', (event) => {
console.error('未处理的 Promise 拒绝:', event.reason);
});
// 网络请求状态管理
class RequestManager {
constructor() {
this.pendingRequests = new Set();
this.isInitialRequestsCompleted = false;
this.lastRequestCompletedTime = null;
this.requestCompletionTimer = null;
this.relayRequestCompletedTime = 300;
}
checkRequestsCompletion() {
const now = Date.now();
if (this.lastRequestCompletedTime && (now - this.lastRequestCompletedTime) >= this.relayRequestCompletedTime) {
this.isInitialRequestsCompleted = true;
}
}
resetCompletionTimer() {
if (this.requestCompletionTimer) {
clearTimeout(this.requestCompletionTimer);
}
this.lastRequestCompletedTime = Date.now();
this.requestCompletionTimer = setTimeout(() => this.checkRequestsCompletion(), this.relayRequestCompletedTime);
}
handleRequestStarted(requestId) {
this.pendingRequests.add(requestId);
}
handleRequestCompleted(requestId, isInitialRequestsCompleted) {
this.pendingRequests.delete(requestId);
this.resetCompletionTimer();
if (isInitialRequestsCompleted) {
this.isInitialRequestsCompleted = true;
}
}
handleRequestFailed(requestId) {
this.pendingRequests.delete(requestId);
this.resetCompletionTimer();
}
isRequestsCompleted() {
return this.lastRequestCompletedTime &&
(Date.now() - this.lastRequestCompletedTime) >= this.relayRequestCompletedTime;
}
getPendingRequestsCount() {
return this.pendingRequests.size;
}
getWaitTimeInSeconds() {
if (!this.lastRequestCompletedTime) return 0;
return Math.floor((this.relayRequestCompletedTime - (Date.now() - this.lastRequestCompletedTime)) / 1000);
}
}
const requestManager = new RequestManager();
// 监听来自 background.js 的网络请求状态更新
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// 处理网络请求状态更新
if (message.type === 'REQUEST_STARTED') {
requestManager.handleRequestStarted(message.requestId);
}
else if (message.type === 'REQUEST_COMPLETED') {
requestManager.handleRequestCompleted(message.requestId, message.isInitialRequestsCompleted);
}
else if (message.type === 'REQUEST_FAILED') {
requestManager.handleRequestFailed(message.requestId);
// console.log('请求失败,待处理请求数:', requestManager.getPendingRequestsCount());
}
return true;
});
// 修改 waitForContent 函数
async function waitForContent() {
return new Promise((resolve) => {
const checkContent = () => {
// 检查是否有主要内容元素
const mainElements = document.querySelectorAll('body, p, h2, article, [role="article"], [role="main"], [data-testid="tweet"]');
// 检查网络请求是否都已完成
const requestsCompleted = requestManager.isRequestsCompleted();
if (mainElements.length > 0 && requestsCompleted) {
// console.log(`页面内容已加载,网络请求已完成(已稳定${requestManager.relayRequestCompletedTime}ms无新请求)`);
resolve();
} else {
const reason = [];
if (mainElements.length === 0) reason.push('主要内容未找到');
if (!requestsCompleted) {
const pendingCount = requestManager.getPendingRequestsCount();
if (pendingCount > 0) {
reason.push(`还有 ${pendingCount} 个网络请求未完成`);
}
const waitTime = requestManager.getWaitTimeInSeconds();
if (waitTime > 0) {
reason.push(`等待请求稳定,剩余 ${waitTime} 秒`);
} else if (!requestManager.lastRequestCompletedTime) {
reason.push('等待首个请求完成');
}
}
// console.log('等待页面加载...', reason.join(', '));
setTimeout(checkContent, 1000);
}
};
// 开始检查
setTimeout(checkContent, 1000);
});
}
// 修改 extractPageContent 函数
async function extractPageContent() {
// console.log('extractPageContent 开始提取页面内容');
// 检查是否是PDF或者iframe中的PDF
if (document.contentType === 'application/pdf' ||
(window.location.href.includes('.pdf') ||
document.querySelector('iframe[src*="pdf.js"]') ||
document.querySelector('iframe[src*=".pdf"]'))) {
// console.log('检测到PDF文件,尝试提取PDF内容');
let pdfUrl = window.location.href;
// 如果是iframe中的PDF,尝试提取实际的PDF URL
const pdfIframe = document.querySelector('iframe[src*="pdf.js"]') || document.querySelector('iframe[src*=".pdf"]');
if (pdfIframe) {
const iframeSrc = pdfIframe.src;
// 尝试从iframe src中提取实际的PDF URL
const urlMatch = iframeSrc.match(/[?&]file=([^&]+)/);
if (urlMatch) {
pdfUrl = decodeURIComponent(urlMatch[1]);
console.log('从iframe中提取到PDF URL:', pdfUrl);
}
}
const pdfText = await extractTextFromPDF(pdfUrl);
if (pdfText) {
return {
title: document.title,
url: window.location.href,
content: pdfText
};
}
}
// 等待内容加载和网络请求完成
await waitForContent();
// 创建一个文档片段来处理内容
const tempContainer = document.createElement('div');
tempContainer.innerHTML = document.body.innerHTML;
// 移除不需要的元素
const selectorsToRemove = [
'script', 'style', 'nav', 'header', 'footer',
'iframe', 'noscript', 'img', 'svg', 'video',
'[role="complementary"]', '[role="navigation"]',
'.sidebar', '.nav', '.footer', '.header'
];
selectorsToRemove.forEach(selector => {
const elements = tempContainer.querySelectorAll(selector);
elements.forEach(element => element.remove());
});
let mainContent = tempContainer.innerText;
// 理文本
mainContent = mainContent
.replace(/\s+/g, ' ') // 替换多个空白字符为单个空格
.replace(/\n\s*\n/g, '\n') // 替换多个换行为单个换行
.trim();
// 检查提取的内容是否足够
if (mainContent.length < 40) {
console.log('提取的内容太少,返回 null');
return null;
}
// console.log('页面内容提取完成,内容:', mainContent);
const gptTokenCount = await estimateGPTTokens(mainContent);
console.log('页面内容提取完成,内容长度:', mainContent.length, 'GPT tokens:', gptTokenCount);
return {
title: document.title,
url: window.location.href,
content: mainContent
};
}
// PDF.js 库的路径
const PDFJS_PATH = chrome.runtime.getURL('lib/pdf.js');
const PDFJS_WORKER_PATH = chrome.runtime.getURL('lib/pdf.worker.js');
// 设置 PDF.js worker 路径
pdfjsLib.GlobalWorkerOptions.workerSrc = PDFJS_WORKER_PATH;
async function extractTextFromPDF(url) {
try {
// 使用已存在的 sidebar 实例
if (!sidebar || !sidebar.sidebar) {
console.error('侧边栏实例不存在');
return null;
}
// 获取iframe
const iframe = sidebar.sidebar.querySelector('.cerebr-sidebar__iframe');
if (!iframe) {
console.error('找不到iframe元素');
return null;
}
// 发送更新placeholder消息
const sendPlaceholderUpdate = (message, timeout = 0) => {
// console.log('发送placeholder更新:', message);
iframe.contentWindow.postMessage({
type: 'UPDATE_PLACEHOLDER',
placeholder: message,
timeout: timeout
}, '*');
};
sendPlaceholderUpdate('正在下载PDF文件...');
console.log('开始下载PDF:', url);
// 首先获取PDF文件的初始信息
const initResponse = await chrome.runtime.sendMessage({
action: 'downloadPDF',
url: url
});
if (!initResponse.success) {
console.error('PDF初始化失败,响应:', initResponse);
sendPlaceholderUpdate('PDF下载失败', 2000);
throw new Error('PDF初始化失败');
}
const { totalChunks, totalSize } = initResponse;
// console.log(`PDF文件大小: ${totalSize} bytes, 总块数: ${totalChunks}`);
// 分块接收数据
const chunks = new Array(totalChunks);
for (let i = 0; i < totalChunks; i++) {
sendPlaceholderUpdate(`正在下载PDF文件 (${Math.round((i + 1) / totalChunks * 100)}%)...`);
const chunkResponse = await chrome.runtime.sendMessage({
action: 'getPDFChunk',
url: url,
chunkIndex: i
});
if (!chunkResponse.success) {
sendPlaceholderUpdate('PDF下载失败', 2000);
throw new Error(`获取PDF块 ${i} 失败`);
}
chunks[i] = new Uint8Array(chunkResponse.data);
}
// 合并所有块
const completeData = new Uint8Array(totalSize);
let offset = 0;
for (const chunk of chunks) {
completeData.set(chunk, offset);
offset += chunk.length;
}
sendPlaceholderUpdate('正在解析PDF文件...');
// console.log('开始解析PDF文件');
const loadingTask = pdfjsLib.getDocument({data: completeData});
const pdf = await loadingTask.promise;
// console.log('PDF加载成功,总页数:', pdf.numPages);
let fullText = '';
// 遍历所有页面
for (let i = 1; i <= pdf.numPages; i++) {
sendPlaceholderUpdate(`正在提取文本 (${i}/${pdf.numPages})...`);
// console.log(`开始处理第 ${i}/${pdf.numPages} 页`);
const page = await pdf.getPage(i);
const textContent = await page.getTextContent();
const pageText = textContent.items.map(item => item.str).join(' ');
// console.log(`第 ${i} 页提取的文本长度:`, pageText.length);
fullText += pageText + '\n';
}
// 计算GPT分词数量
const gptTokenCount = await estimateGPTTokens(fullText);
console.log('PDF文本提取完成,总文本长度:', fullText.length, '预计GPT tokens:', gptTokenCount);
sendPlaceholderUpdate(`PDF处理完成 (约 ${gptTokenCount} tokens)`, 2000);
return fullText;
} catch (error) {
console.error('PDF处理过程中出错:', error);
console.error('错误堆栈:', error.stack);
if (sidebar && sidebar.sidebar) {
const iframe = sidebar.sidebar.querySelector('.cerebr-sidebar__iframe');
if (iframe) {
iframe.contentWindow.postMessage({
type: 'UPDATE_PLACEHOLDER',
placeholder: 'PDF处理失败',
timeout: 2000
}, '*');
}
}
return null;
}
}
// 添加GPT分词估算函数
async function estimateGPTTokens(text) {
try {
// 简单估算:平均每4个字符约为1个token
// 这是一个粗略估计,实际token数可能会有所不同
const estimatedTokens = Math.ceil(text.length / 4.25625);
return estimatedTokens;
} catch (error) {
console.error('计算GPT tokens时出错:', error);
return 0;
}
}