-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
792 lines (710 loc) · 25.2 KB
/
Copy pathbackground.js
File metadata and controls
792 lines (710 loc) · 25.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
/**
* BiViNote Background Script (Service Worker)
* 代理 Bilibili API 请求,处理 CORS,管理图标状态
*/
// ── 图标状态管理 ──
function isVideoPage(url) {
try {
const parsed = new URL(url);
return parsed.hostname.includes('bilibili.com') &&
(parsed.pathname.startsWith('/video/') || parsed.pathname.startsWith('/list/'));
} catch {
return false;
}
}
function updateIconForTab(tabId, url) {
const enabled = isVideoPage(url || '');
if (enabled) {
chrome.action.setIcon({
tabId,
path: {
16: 'icons/icon-16.png',
32: 'icons/icon-32.png',
48: 'icons/icon-48.png',
128: 'icons/icon-128.png'
}
}).catch(() => {});
chrome.action.setTitle({ tabId, title: 'BiViNote - 点击打开' }).catch(() => {});
} else {
chrome.action.setIcon({
tabId,
path: {
16: 'icons/icon-16-disabled.png',
32: 'icons/icon-32-disabled.png',
48: 'icons/icon-48-disabled.png',
128: 'icons/icon-128-disabled.png'
}
}).catch(() => {});
chrome.action.setTitle({ tabId, title: 'BiViNote - 仅在B站视频页可用' }).catch(() => {});
}
}
// 监听标签页 URL 变化
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.url || changeInfo.status === 'complete') {
updateIconForTab(tabId, tab.url);
}
});
// 监听标签页切换
chrome.tabs.onActivated.addListener(async ({ tabId }) => {
try {
const tab = await chrome.tabs.get(tabId);
updateIconForTab(tabId, tab.url);
} catch {}
});
// 扩展安装/更新时,刷新所有标签页图标
chrome.runtime.onInstalled.addListener(async () => {
try {
const tabs = await chrome.tabs.query({});
for (const tab of tabs) {
if (tab.id && tab.url) {
updateIconForTab(tab.id, tab.url);
}
}
} catch {}
});
// ── 消息监听 ──
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'fetch-video-meta') {
fetchVideoMeta(message.bvid)
.then(data => sendResponse({ ok: true, data }))
.catch(err => sendResponse({ ok: false, error: err.message }));
return true;
}
if (message.type === 'fetch-subtitle-list') {
fetchSubtitleList(message.bvid, message.cid, message.aid)
.then(data => sendResponse({ ok: true, data }))
.catch(err => sendResponse({ ok: false, error: err.message }));
return true;
}
if (message.type === 'fetch-subtitle-body') {
fetchSubtitleBody(message.url)
.then(data => sendResponse({ ok: true, data }))
.catch(err => sendResponse({ ok: false, error: err.message }));
return true;
}
// 打开选项页面
if (message.type === 'open-options') {
if (message.section) {
chrome.tabs.create({ url: chrome.runtime.getURL(`options.html?section=${message.section}`) });
} else {
chrome.runtime.openOptionsPage();
}
return false;
}
// ── DeepSeek 文档整理 ──
if (message.type === 'ds-check-login') {
dsCheckLogin().then(result => sendResponse(result));
return true;
}
if (message.type === 'ds-abort') {
const abortRid = message.requestId;
// 只终止指定请求的处理器,不影响其他并行任务
if (abortRid && dsSseProcessors[abortRid]) {
const processor = dsSseProcessors[abortRid];
const chatId = processor.getChatId();
const messageId = processor.getMessageId();
chrome.tabs.query({ url: '*://chat.deepseek.com/*' }, (tabs) => {
if (tabs[0]?.id) {
chrome.tabs.sendMessage(tabs[0].id, {
type: 'ds-abort-stop',
chatId,
messageId,
}).catch(() => {});
}
});
delete dsSseProcessors[abortRid];
} else {
// 无 requestId 时降级:终止第一个处理器(向后兼容)
const processorIds = Object.keys(dsSseProcessors);
if (processorIds.length > 0) {
const processor = dsSseProcessors[processorIds[0]];
const chatId = processor.getChatId();
const messageId = processor.getMessageId();
chrome.tabs.query({ url: '*://chat.deepseek.com/*' }, (tabs) => {
if (tabs[0]?.id) {
chrome.tabs.sendMessage(tabs[0].id, {
type: 'ds-abort-stop',
chatId,
messageId,
}).catch(() => {});
}
});
}
dsSseProcessors = {};
}
return false;
}
if (message.type === 'ds-send') {
const requestId = message.requestId || crypto.randomUUID();
dsHandleSend(message.markdown, message.prompt, requestId, message.thinking, message.taskId || 'clear');
sendResponse({ ok: true, requestId });
return true;
}
if (message.type === 'ds-open-login') {
chrome.tabs.query({ url: '*://chat.deepseek.com/*' }, (tabs) => {
if (tabs.length > 0) {
chrome.tabs.update(tabs[0].id, { active: true });
} else {
chrome.tabs.create({ url: 'https://chat.deepseek.com' });
}
});
return false;
}
if (message.type === 'ds-open-chat') {
const url = message.url || 'https://chat.deepseek.com';
const isStreaming = Object.keys(dsSseProcessors).length > 0;
chrome.tabs.query({ url: '*://chat.deepseek.com/*' }, (tabs) => {
if (isStreaming || tabs.length === 0) {
// 流式处理中或无标签页:新建标签页,避免中断正在运行的脚本
chrome.tabs.create({ url });
} else {
// 空闲:复用已有标签页
chrome.tabs.update(tabs[0].id, { url, active: true });
}
});
return false;
}
// DeepSeek bridge → bilibili tab 转发
if (message.type === 'DEEPSEEK_CHUNK') {
const rid = message.requestId;
if (!dsSseProcessors[rid]) dsSseProcessors[rid] = dsCreateSSEProcessor(rid);
const processor = dsSseProcessors[rid];
const text = processor.processChunk(message.chunk);
dsSendToBilibiliTab({ type: 'ds-chunk', text, requestId: rid, chatId: processor.getChatId() });
return false;
}
if (message.type === 'DEEPSEEK_DONE') {
const rid = message.requestId;
if (dsSseProcessors[rid]) {
const tail = dsSseProcessors[rid].flush();
if (tail) dsSendToBilibiliTab({ type: 'ds-chunk', text: tail, requestId: rid });
delete dsSseProcessors[rid];
}
dsSendToBilibiliTab({ type: 'ds-done', requestId: rid });
return false;
}
if (message.type === 'DEEPSEEK_ERROR') {
delete dsSseProcessors[message.requestId];
dsSendToBilibiliTab({ type: 'ds-error', error: message.error, requestId: message.requestId });
return false;
}
});
// 扩展图标点击 → 无操作(面板自动显示)
chrome.action.onClicked.addListener((tab) => {
// 无操作
});
/**
* 获取视频元信息
*/
async function fetchVideoMeta(bvid) {
const url = `https://api.bilibili.com/x/web-interface/view?bvid=${encodeURIComponent(bvid)}`;
const payload = await fetchJson(url);
if (payload.code !== 0) {
throw new Error(payload?.message || '无法获取视频信息');
}
const data = payload.data || {};
const pubdate = Number(data.pubdate || 0);
const uploadDate = pubdate > 0 ? formatDate(pubdate * 1000) : '';
const pages = Array.isArray(data.pages) ? data.pages : [];
return {
aid: data.aid ? String(data.aid) : '',
title: String(data.title || ''),
author: String(data.owner?.name || ''),
description: String(data.desc || ''),
uploadDate,
defaultCid: data.cid ? String(data.cid) : '',
defaultDuration: Number(data.duration || 0) || 0,
pages: pages.map(item => ({
cid: String(item.cid || ''),
page: Number(item.page || 0) || 0,
part: String(item.part || '').trim(),
duration: Number(item.duration || 0) || 0
}))
};
}
/**
* 获取字幕列表和章节
* 完全遵循 Bilibili-Obsidian-Clipper 的双源策略:
* 1. 主源:player/wbi/v2?aid=xxx&cid=xxx (用 aid 作为主标识)
* 2. 回退:player/v2?bvid=xxx&cid=xxx
*/
async function fetchSubtitleList(bvid, cid, aid = '') {
const requests = buildSubtitleInfoRequests({ bvid, cid, aid });
for (const request of requests) {
try {
const payload = await fetchJson(request.url);
if (payload.code !== 0) {
console.warn(`[BiViNote] ${request.source} failed:`, payload?.message);
continue;
}
const data = payload.data || {};
const subtitles = normalizeSubtitleTracks(
(data.subtitle?.subtitles || []).map(item => ({
id: item?.id === undefined || item?.id === null ? '' : String(item.id),
lan: item?.lan || '',
lanDoc: item?.lan_doc || '',
subtitleUrl: normalizeSubtitleUrl(item?.subtitle_url || ''),
source: request.source
})).filter(item => item.subtitleUrl)
);
const chapters = normalizeChapters(
(data.view_points || []).map(item => ({
title: String(item?.content || item?.title || item?.label || '').trim(),
from: normalizeChapterTime(item?.from ?? item?.start ?? item?.start_time),
to: normalizeChapterTime(item?.to ?? item?.end ?? item?.end_time)
}))
);
return { subtitles, chapters };
} catch (err) {
console.warn(`[BiViNote] ${request.source} error:`, err.message);
continue;
}
}
// 所有源都失败
return { subtitles: [], chapters: [] };
}
/**
* 构建字幕 API 请求列表(双源策略)
*/
function buildSubtitleInfoRequests({ bvid, cid, aid }) {
const safeBvid = encodeURIComponent(String(bvid || ''));
const safeCid = encodeURIComponent(String(cid || ''));
const safeAid = encodeURIComponent(String(aid || ''));
const requests = [];
// 主源:player/wbi/v2 用 aid 作为主标识
if (aid) {
requests.push({
source: 'player-wbi-v2',
url: `https://api.bilibili.com/x/player/wbi/v2?aid=${safeAid}&cid=${safeCid}&bvid=${safeBvid}`
});
}
// 回退:player/v2 用 bvid 作为主标识
requests.push({
source: 'player-v2',
url: `https://api.bilibili.com/x/player/v2?bvid=${safeBvid}&cid=${safeCid}` +
(aid ? `&aid=${safeAid}` : '')
});
return requests;
}
/**
* 获取字幕正文
* CDN 域名 (hdslb.com) 响应头为 Access-Control-Allow-Origin: *
* 不能带 credentials,否则 CORS 报错
*/
async function fetchSubtitleBody(url) {
const normalizedUrl = normalizeSubtitleUrl(url);
const isCdn = normalizedUrl.includes('hdslb.com');
const resp = await fetch(normalizedUrl, {
credentials: isCdn ? 'omit' : 'include',
cache: 'no-store'
});
if (!resp.ok) {
throw new Error(`字幕请求失败:${resp.status}`);
}
const data = await resp.json();
return Array.isArray(data.body) ? data.body : [];
}
/**
* 通用 JSON 请求
*/
async function fetchJson(url) {
const resp = await fetch(url, { credentials: 'include', cache: 'no-store' });
if (!resp.ok) {
throw new Error(`请求失败:${resp.status}`);
}
return resp.json();
}
/**
* 标准化字幕 URL
*/
function normalizeSubtitleUrl(url) {
if (!url) return '';
if (url.startsWith('//')) return `https:${url}`;
if (url.startsWith('http://') || url.startsWith('https://')) return url;
return `https://${url.replace(/^\/+/, '')}`;
}
/**
* 字幕语言优先级(数值越小越优先)
* 中文 > 英文 > 其他
*/
function subtitlePriority(item) {
const lan = String(item?.lan || '').toLowerCase();
const label = String(item?.lanDoc || '').toLowerCase();
if (lan === 'zh-cn' || lan === 'zh-hans') return 0;
if (lan === 'zh') return 1;
if (lan.includes('zh')) return 2;
if (label.includes('中文')) return 3;
if (lan === 'en' || lan === 'en-us' || lan === 'en-gb') return 10;
if (lan.includes('en')) return 11;
if (label.includes('英文') || label.includes('英语') || label.includes('english')) return 12;
return 50;
}
/**
* 提取 URL 的稳定部分(去掉 auth_key 等动态参数)
*/
function urlPathKey(url) {
try {
return new URL(url).pathname;
} catch {
return String(url || '').split('?')[0];
}
}
/**
* 按语言优先级排序字幕轨道,保证每次顺序一致
*/
function normalizeSubtitleTracks(subtitles) {
return [...(subtitles || [])].sort((a, b) => {
const p = subtitlePriority(a) - subtitlePriority(b);
if (p !== 0) return p;
const lanA = String(a.lanDoc || a.lan || '').toLowerCase();
const lanB = String(b.lanDoc || b.lan || '').toLowerCase();
if (lanA < lanB) return -1;
if (lanA > lanB) return 1;
const idA = Number.parseInt(String(a.id || '0'), 10);
const idB = Number.parseInt(String(b.id || '0'), 10);
if (Number.isFinite(idA) && Number.isFinite(idB) && idA !== idB) return idA - idB;
// 用 URL path 比较,忽略 auth_key 等动态查询参数
return urlPathKey(a.subtitleUrl).localeCompare(urlPathKey(b.subtitleUrl));
});
}
/**
* 标准化章节时间
*/
function normalizeChapterTime(value) {
if (value === undefined || value === null || value === '') return 0;
const num = Number(value);
if (!Number.isFinite(num) || num < 0) return 0;
return num > 60 * 60 * 24 ? num / 1000 : num;
}
/**
* 标准化章节列表
*/
function normalizeChapters(chapters) {
const normalized = (chapters || [])
.map(item => ({
title: String(item?.title || '').trim(),
from: Number(item?.from || 0) || 0,
to: Number(item?.to || 0) || 0
}))
.filter(item => item.title && item.from >= 0)
.sort((a, b) => a.from - b.from);
// 去重
const unique = [];
const seen = new Set();
for (const item of normalized) {
const key = `${Math.floor(item.from * 10)}|${item.title.toLowerCase()}`;
if (!seen.has(key)) {
seen.add(key);
unique.push(item);
}
}
return unique;
}
/**
* 格式化日期
*/
function formatDate(timestamp) {
const d = new Date(timestamp);
const pad = n => String(n).padStart(2, '0');
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
}
// ── DeepSeek 文档整理 ──
const DS_URL = 'https://chat.deepseek.com';
let dsInjectedTabs = new Set();
let chatIds = { ds: null, summary: null };
let dsSseProcessors = {};
let dsSenderTabId = null;
let dsRequestIdToTaskId = {}; // requestId -> taskId 映射
// 恢复 chatId
chrome.storage.local.get(['chatId_clear', 'chatId_summary'], (stored) => {
if (stored.chatId_clear) chatIds.clear = stored.chatId_clear;
if (stored.chatId_summary) chatIds.summary = stored.chatId_summary;
});
async function dsEnsureTab() {
const tabs = await chrome.tabs.query({ url: '*://chat.deepseek.com/*' });
if (tabs.length > 0 && tabs[0].id) return tabs[0];
const tab = await chrome.tabs.create({ url: DS_URL, active: false });
await dsWaitTabComplete(tab.id, 20000);
return tab;
}
async function dsWaitTabComplete(tabId, timeout) {
try {
const tab = await chrome.tabs.get(tabId);
if (tab.status === 'complete') return;
} catch { return; }
await new Promise((resolve) => {
const listener = (tid, changeInfo) => {
if (tid === tabId && changeInfo.status === 'complete') {
chrome.tabs.onUpdated.removeListener(listener);
resolve();
}
};
chrome.tabs.onUpdated.addListener(listener);
setTimeout(() => { chrome.tabs.onUpdated.removeListener(listener); resolve(); }, timeout);
});
}
async function dsInjectScripts(tabId) {
if (dsInjectedTabs.has(tabId)) return;
await chrome.scripting.executeScript({ target: { tabId }, world: 'ISOLATED', files: ['libs/deepseek-bridge.js'] });
await chrome.scripting.executeScript({ target: { tabId }, world: 'MAIN', files: ['libs/wasm-solver.js'] });
await chrome.scripting.executeScript({ target: { tabId }, world: 'MAIN', files: ['libs/deepseek-api.js'] });
dsInjectedTabs.add(tabId);
}
chrome.tabs.onRemoved.addListener((tabId) => { dsInjectedTabs.delete(tabId); });
async function dsCheckLogin() {
try {
// 自动获取或创建 DeepSeek 标签页
const tab = await dsEnsureTab();
if (!tab?.id) return { loggedIn: false };
// 主要方式:通过 localStorage token 验证
let result = await tryCheckLoginWithToken(tab.id);
if (result && typeof result.loggedIn === 'boolean') {
// 如果检测失败,激活标签页并刷新后重试
if (!result.loggedIn) {
await chrome.tabs.update(tab.id, { active: true });
await chrome.tabs.reload(tab.id);
await dsWaitTabComplete(tab.id, 10000);
result = await tryCheckLoginWithToken(tab.id);
}
return result;
}
// 降级:cookie 检测
const cookies = await chrome.cookies.getAll({ domain: '.deepseek.com' });
const cookieMap = Object.fromEntries(cookies.map(c => [c.name, c.value]));
const hasSession = ['ds_session_id', 'HWSID'].some(name => !!cookieMap[name]);
if (hasSession) return { loggedIn: true, key: 'cookie' };
return { loggedIn: false };
} catch (e) {
return { loggedIn: false, reason: String(e) };
}
}
async function tryCheckLoginWithToken(tabId) {
try {
const results = await chrome.scripting.executeScript({
target: { tabId },
world: 'MAIN',
func: async () => {
function tryGetToken(raw) {
if (!raw || typeof raw !== 'string' || raw.length <= 10) return null;
try {
const parsed = JSON.parse(raw);
if (typeof parsed === 'string' && parsed.length > 10) return parsed;
if (typeof parsed === 'object' && parsed !== null) {
const t = parsed.token || parsed.value || parsed.access_token || parsed.jwt;
return (t && typeof t === 'string' && t.length > 10) ? t : null;
}
} catch { return raw; }
return null;
}
const keys = ['userToken', 'token', 'ds_token', 'auth_token', 'access_token', 'jwt'];
let token = null;
for (const key of keys) {
token = tryGetToken(localStorage.getItem(key));
if (token) break;
}
if (!token) return { loggedIn: false };
try {
const res = await fetch('https://chat.deepseek.com/api/v1/user/profile', {
method: 'GET',
headers: { 'Authorization': 'Bearer ' + token },
credentials: 'include',
});
return { loggedIn: res.ok };
} catch { return null; }
},
});
return results?.[0]?.result || null;
} catch {
return null;
}
}
function dsSendToBilibiliTab(msg) {
if (!dsSenderTabId) return;
chrome.tabs.sendMessage(dsSenderTabId, msg).catch(() => {});
}
async function dsHandleSend(markdown, prompt, requestId, thinking, taskId = 'clear') {
// 不清空 dsSseProcessors,保留运行中任务的处理器状态(如 inThink)
dsRequestIdToTaskId[requestId] = taskId;
const activeTabs = await chrome.tabs.query({ active: true, currentWindow: true });
if (activeTabs[0]?.id) dsSenderTabId = activeTabs[0].id;
let tab;
try {
tab = await dsEnsureTab();
} catch (e) {
dsSendToBilibiliTab({ type: 'ds-error', error: `获取标签页失败: ${String(e)}`, requestId });
return;
}
try {
await dsInjectScripts(tab.id);
} catch (e) {
dsSendToBilibiliTab({ type: 'ds-error', error: `注入脚本失败: ${String(e)}`, requestId });
return;
}
// 处理 {markdown} 占位符
let fullPrompt = prompt;
if (fullPrompt.includes('{markdown}')) {
fullPrompt = fullPrompt.replace('{markdown}', markdown);
} else {
fullPrompt = fullPrompt + '\n\n' + markdown;
}
const chatId = chatIds[taskId] || null;
chrome.tabs.sendMessage(tab.id, {
type: 'ds-inject-request',
payload: { prompt: fullPrompt, chatId, requestId, thinking, taskId }
}).catch((e) => {
if (String(e).includes('Receiving end does not exist')) {
dsInjectedTabs.delete(tab.id);
dsInjectScripts(tab.id).then(() => {
chrome.tabs.sendMessage(tab.id, {
type: 'ds-inject-request',
payload: { prompt: fullPrompt, chatId, requestId, thinking, taskId }
});
});
} else {
dsSendToBilibiliTab({ type: 'ds-error', error: `发送请求失败: ${String(e)}`, requestId });
}
});
}
function dsCreateSSEProcessor(requestId) {
let inThink = false;
let chatId = null;
let messageId = null;
let dataLineBuf = '';
const taskId = dsRequestIdToTaskId[requestId] || 'clear';
function processChunk(chunk) {
let text = '';
try {
for (const line of chunk.split('\n')) {
if (line.startsWith('data: ')) {
const jsonStr = line.slice(6).trim();
if (!jsonStr || jsonStr === '[DONE]') { dataLineBuf = ''; continue; }
try {
const data = JSON.parse(jsonStr);
dataLineBuf = '';
if (data.type === 'deepseek:chat_session_id') {
chatId = data.chat_session_id;
// 更新对应任务的 chatId
chatIds[taskId] = chatId;
let storageKey;
if (taskId === 'clear') storageKey = 'chatId_clear';
else if (taskId === 'summary') storageKey = 'chatId_summary';
else storageKey = 'chatId_' + taskId;
chrome.storage.local.set({ [storageKey]: chatId });
continue;
}
if (data.response_message_id != null) messageId = data.response_message_id;
const result = processEvent(data);
if (result) text += result;
} catch {
dataLineBuf = line;
}
} else {
if (dataLineBuf) {
dataLineBuf += '\n' + line;
try {
const jsonStr = dataLineBuf.slice(6).trim();
const data = JSON.parse(jsonStr);
dataLineBuf = '';
if (data.type === 'deepseek:chat_session_id') {
chatId = data.chat_session_id;
// 更新对应任务的 chatId
chatIds[taskId] = chatId;
let storageKey;
if (taskId === 'clear') storageKey = 'chatId_clear';
else if (taskId === 'summary') storageKey = 'chatId_summary';
else storageKey = 'chatId_' + taskId;
chrome.storage.local.set({ [storageKey]: chatId });
continue;
}
if (data.response_message_id != null) messageId = data.response_message_id;
const result = processEvent(data);
if (result) text += result;
} catch {}
}
}
}
} catch {}
return text;
}
function processEvent(data) {
if (data.o === 'SET' || data.o === 'BATCH') return null;
const path = Array.isArray(data.p) ? data.p.join('/') : data.p;
const resp = data.v?.response;
if (resp?.fragments && Array.isArray(resp.fragments)) {
const parts = [];
for (const frag of resp.fragments) {
const content = frag.content || '';
if (!content) continue;
if (frag.type === 'THINK' || frag.type === 'THINKING' || frag.type === 'reasoning') {
if (!inThink) { inThink = true; parts.push('<think>'); }
parts.push(content);
} else {
if (inThink) { inThink = false; parts.push('</think>'); }
parts.push(content);
}
}
return parts.length > 0 ? parts.join('') : null;
}
if (data.o === 'APPEND' && Array.isArray(data.v)) {
const parts = [];
for (const item of data.v) {
const content = item.content || '';
if (item.type === 'THINK' || item.type === 'THINKING' || item.type === 'reasoning') {
if (!inThink) { inThink = true; parts.push('<think>'); }
if (content) parts.push(content);
} else if (item.type === 'RESPONSE' || item.type === 'TEXT' || item.type === 'text') {
if (inThink) { inThink = false; parts.push('</think>'); }
if (content) parts.push(content);
} else {
if (content) parts.push(content);
}
}
return parts.length > 0 ? parts.join('') : null;
}
if (data.o === 'APPEND' && typeof data.v === 'string') {
return data.v || null;
}
if (path?.includes('reasoning') && typeof data.v === 'string') {
if (!data.v) return null;
if (!inThink) { inThink = true; return `<think>${data.v}`; }
return data.v;
}
if (data.type === 'thinking') {
const content = typeof data.v === 'string' ? data.v : data.content || '';
if (!content) return null;
if (!inThink) { inThink = true; return `<think>${content}`; }
return content;
}
const delta = data.choices?.[0]?.delta;
if (delta) {
const parts = [];
if (delta.reasoning_content) {
if (!inThink) { inThink = true; parts.push('<think>'); }
parts.push(delta.reasoning_content);
}
if (delta.content) {
if (inThink) { inThink = false; parts.push('</think>'); }
parts.push(delta.content);
}
return parts.length > 0 ? parts.join('') : null;
}
if (typeof data.v === 'string') {
if (!data.v) return null;
if (!path && inThink) return data.v;
if (inThink) { inThink = false; return `</think>${data.v}`; }
return data.v;
}
if (data.type === 'text' && typeof data.content === 'string') {
const content = data.content.trim();
if (!content) return null;
if (inThink) { inThink = false; return `</think>${content}`; }
return content;
}
return null;
}
function flush() {
if (inThink) { inThink = false; return '</think>'; }
return '';
}
return { processChunk, flush, getChatId: () => chatId, getMessageId: () => messageId };
}