-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
1248 lines (1141 loc) · 45.7 KB
/
Copy pathbackground.js
File metadata and controls
1248 lines (1141 loc) · 45.7 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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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 (details) => {
try {
const tabs = await chrome.tabs.query({});
for (const tab of tabs) {
if (tab.id && tab.url) {
updateIconForTab(tab.id, tab.url);
}
}
// 更新后重载已打开的 chat.deepseek.com 标签页:其 window.__deepseekApiInjected
// 守卫会让旧脚本拒绝重新注入,不重载则升级后仍发送旧请求体(无 model_type 等新参数)
if (details && details.reason === 'update') {
const dsTabs = await chrome.tabs.query({ url: '*://chat.deepseek.com/*' });
for (const t of dsTabs) {
if (t.id != null) chrome.tabs.reload(t.id).catch(() => {});
}
}
} 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.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;
}
// 查看笔记:打开 B站笔记列表。同链接已在某标签页则激活并刷新(列表页是旧内容,
// 不刷新看不到刚保存的最新笔记),否则新建标签页
if (message.type === 'bn-open-note') {
const url = String(message.url || '');
if (!url) return false;
chrome.tabs.query({ url: '*://*.bilibili.com/*' }, (tabs) => {
const hit = tabs.find((t) => t.url === url);
if (hit && hit.id != null) {
chrome.tabs.update(hit.id, { active: true });
if (hit.windowId != null) chrome.windows.update(hit.windowId, { focused: true });
chrome.tabs.reload(hit.id);
} else {
chrome.tabs.create({ url });
}
});
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 processor = dsSseProcessors[rid];
const fatal = processor.getError();
if (fatal) {
// 服务端拒绝(如 expert 客户端版本过旧):报错而非「完成但空白」
delete dsSseProcessors[rid];
dsSendToBilibiliTab({ type: 'ds-error', error: fatal, requestId: rid });
return false;
}
const tail = processor.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;
}
// ── B站笔记保存(记笔记):注入视频页 MAIN world 执行 ──
if (message.type === 'bn-note-save') {
const tabId = sender.tab?.id;
if (!tabId) {
sendResponse({ ok: false, error: '请在 bilibili 视频页使用' });
return false;
}
chrome.scripting.executeScript({
target: { tabId },
world: 'MAIN',
func: bnSaveNoteMain,
args: [message.payload],
})
.then((injectionResults) => {
const result = injectionResults?.[0]?.result || { ok: false, step: 'inject', error: '未获得执行结果' };
sendResponse({ ok: !!result.ok, result });
})
.catch((err) => sendResponse({ ok: false, error: String((err && err.message) || err) }));
return true; // async sendResponse
}
// ── 发评论:注入视频页 MAIN world 执行 ──
if (message.type === 'bn-send-comment') {
const tabId = sender.tab?.id;
if (!tabId) {
sendResponse({ ok: false, error: '请在 bilibili 视频页使用' });
return false;
}
chrome.scripting.executeScript({
target: { tabId },
world: 'MAIN',
func: bnSendCommentMain,
args: [String(message.text || '')],
})
.then((injectionResults) => {
const result = injectionResults?.[0]?.result || { ok: false, step: 'inject', error: '未获得执行结果' };
sendResponse({ ok: !!result.ok, result });
})
.catch((err) => sendResponse({ ok: false, error: String((err && err.message) || err) }));
return true; // async sendResponse
}
});
// 扩展图标点击 → 打开选项页
chrome.action.onClicked.addListener((tab) => {
chrome.runtime.openOptionsPage();
});
/**
* 获取视频元信息
*/
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;
});
// 会话键被外部删除(options 切换模型类型)时同步清内存,避免复用旧模型会话
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName !== 'local') return;
for (const key of Object.keys(changes)) {
if (key.startsWith('chatId_') && changes[key].newValue === undefined) {
delete chatIds[key.slice('chatId_'.length)];
}
}
});
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(() => {});
}
// 解析全局模型配置(文档整理统一使用),expert 模式强制关闭搜索
async function dsResolveModelConfig() {
return new Promise((resolve) => {
chrome.storage.local.get('bivinote_settings', (result) => {
const s = (result && result.bivinote_settings) || {};
const modelType = s.deepseekModelType === 'expert' ? 'expert' : 'default';
const searchEnabled = modelType === 'expert' ? false : s.deepseekSearch === true;
const thinkingEnabled = s.deepseekThinking !== false; // 默认 true
resolve({ modelType, searchEnabled, thinkingEnabled });
});
});
}
async function dsHandleSend(markdown, prompt, requestId, 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} 占位符(严格模式:提示词须显式包含 {markdown} 才会插入文档,
// 否则原样发送提示词、不附加文档)。replaceAll 让同串出现多处时全部替换。
let fullPrompt = prompt;
if (fullPrompt.includes('{markdown}')) {
fullPrompt = fullPrompt.replaceAll('{markdown}', markdown);
}
const chatId = chatIds[taskId] || null;
// 读取全局模型配置(若读取失败,按默认 default + thinking 发送)
let modelConfig = { modelType: 'default', searchEnabled: false, thinkingEnabled: true };
try {
modelConfig = await dsResolveModelConfig();
} catch (e) {
modelConfig = { modelType: 'default', searchEnabled: false, thinkingEnabled: true };
}
chrome.tabs.sendMessage(tab.id, {
type: 'ds-inject-request',
payload: { prompt: fullPrompt, chatId, requestId, taskId,
modelType: modelConfig.modelType,
searchEnabled: modelConfig.searchEnabled,
thinkingEnabled: modelConfig.thinkingEnabled }
}).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, taskId,
modelType: modelConfig.modelType,
searchEnabled: modelConfig.searchEnabled,
thinkingEnabled: modelConfig.thinkingEnabled }
});
});
} else {
dsSendToBilibiliTab({ type: 'ds-error', error: `发送请求失败: ${String(e)}`, requestId });
}
});
}
function dsCreateSSEProcessor(requestId) {
let inThink = false;
let chatId = null;
let messageId = null;
let dataLineBuf = '';
let fatalError = null; // 服务端拒绝类事件(如 unsupported_client_by_model)
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) {
// 服务端拒绝类事件(官方经 event: hint 发来):expert 因客户端版本过旧会返回
// {"type":"error","content":"Update to the latest version...","clear_response":true,
// "finish_reason":"unsupported_client_by_model"},且不产生任何正文。捕获它转成
// ds-error,避免 DEEPSEEK_DONE 后表现为「完成但空白」。
if (data.type === 'error' || data.clear_response === true) {
fatalError = data.content || data.message || data.msg ||
`DeepSeek 拒绝请求(${data.finish_reason || 'unknown'})`;
return null;
}
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, getError: () => fatalError };
}
// ── B站笔记保存(记笔记)────────────────────────────
// 把面板「记笔记」的 payload 注入 bilibili 视频页 MAIN world 执行:
// 主世界 fetch(credentials:'include') 才能带全 HttpOnly SESSDATA;csrf 读 bili_jct。
// payload = { plan, images, noteId, aid, cid, title }
// plan : libs/bili-markup.js toPlan 产物
// {kind:'t', t, a?} 文字(a = bold/underline/strike/size)
// {kind:'nl', list?} 段界(list='bullet'|'ordered')
// {kind:'img', label?} 图片占位(label 形如 '0043.png',按 label 到 images 取图)
// {kind:'tag', seconds} 视频时间点
// images: { [label]: {dataUrl, width} }(content 侧已转好 dataURL;同 label 复用一次上传)
async function bnSaveNoteMain(payload) {
const fail = (step, error, extra) => Object.assign({ ok: false, step, error }, extra || {});
try {
const aid = Number(payload.aid) || 0;
const cid = Number(payload.cid) || 0;
if (!aid || !cid) return fail('meta', '未识别到视频信息(aid/cid),请确认在 bilibili 视频页');
const title = String(payload.title || '').slice(0, 40);
const mc = document.cookie.match(/(?:^|; )bili_jct=([^;]*)/);
const csrf = mc ? decodeURIComponent(mc[1]) : '';
if (!csrf) return fail('csrf', '读取不到 bili_jct,请确认已登录 bilibili');
const tagKeyBase = Date.now();
const plan = Array.isArray(payload.plan) ? payload.plan : [];
const images = (payload.images && typeof payload.images === 'object') ? payload.images : {};
if (!plan.length) return fail('empty', '没有任何内容可保存');
// 预检:plan 里每个 img 的 label 都必须在 images 里
for (const tok of plan) {
if (tok.kind === 'img' && !images[tok.label]) {
return fail('img', '缺少图片 ' + tok.label + ',请重新整理后再保存');
}
}
const uploadCache = {}; // label -> location(同名复用,避免重复上传同一张)
const upload = async (label) => {
if (uploadCache[label]) return uploadCache[label];
const im = images[label];
const blob = await (await fetch(im.dataUrl)).blob();
const fd = new FormData();
fd.append('file', blob);
fd.append('csrf', csrf);
const r = await fetch('https://api.bilibili.com/x/note/image/upload', {
method: 'POST', credentials: 'include', body: fd,
});
const j = await r.json();
if (j.code !== 0) throw new Error('图片上传失败: ' + (j.message || j.code));
uploadCache[label] = j.data.location;
return j.data.location;
};
const ops = []; // 最终 Quill delta ops
const tagObjs = []; // 时间标签(服务端用 tags 串排章节)
let plainBuf = ''; // 纯文本(供 summary / cont_len)
let imgSeq = 0;
for (const tok of plan) {
if (tok.kind === 't') {
const o = { insert: tok.t };
if (tok.a && Object.keys(tok.a).length) o.attributes = tok.a;
ops.push(o);
plainBuf += tok.t;
} else if (tok.kind === 'nl') {
const o = { insert: '\n' };
if (tok.list) o.attributes = { list: tok.list };
ops.push(o);
plainBuf += '\n';
} else if (tok.kind === 'img') {
const location = await upload(tok.label); // 上传一次,复用
imgSeq++;
ops.push({
insert: {
imageUpload: {
url: location, status: 'done', width: images[tok.label].width || 600,
id: 'IMAGE_' + Date.now() + '_' + imgSeq, source: 'video',
},
},
});
// markup.js 已在图片占位后放 nl,此处无需再补
} else if (tok.kind === 'tag') {
// 时间点 chip 与真实编辑器同构:cid=当前视频、index/cidCount 恒 1/1(当前页单分P语义)。
// 若 index/cidCount 按"全文 tag 个数"递增、或漏填 cid,阅读端会渲染成多分P时间轴。
const t = {
cid, oid_type: 0, status: 0, index: 1, seconds: tok.seconds, cidCount: 1,
key: String(tagKeyBase + tagObjs.length),
title, epid: 0, desc: '',
};