-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
90 lines (79 loc) · 2.33 KB
/
Copy pathcontent.js
File metadata and controls
90 lines (79 loc) · 2.33 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
/**
* BiViNote Content Script
* 入口脚本 - 初始化所有模块,监听 SPA 路由变化
*/
(async function () {
'use strict';
const BN = window.BiViNote;
// ── 初始化 ──
async function init() {
try {
// 加载设置
await BN.settings.load();
// 监听 background 消息(toggle-panel)
chrome.runtime.onMessage.addListener((message) => {
if (message.type === 'toggle-panel') {
const s = BN.state;
// 如果有东西显示着,先隐藏
if (s.panelVisible) {
BN.panel.hide();
return;
}
if (s.collapsed) {
BN.panel.hideCollapse();
return;
}
// 都没显示,按上次使用的模式打开
const lastMode = s.settings.lastOpenMode || 'panel';
if (lastMode === 'menu') {
BN.panel.toggleCollapse();
} else {
BN.panel.show();
}
}
});
console.log('[BiViNote] Initialized successfully');
} catch (err) {
console.error('[BiViNote] Initialization failed:', err);
}
}
// ── SPA 路由监听 ──
let lastUrl = location.href;
const urlObserver = new MutationObserver(() => {
if (location.href !== lastUrl) {
lastUrl = location.href;
onRouteChange();
}
});
urlObserver.observe(document.body, { childList: true, subtree: true });
let lastBvid = '';
let lastPage = 1;
function onRouteChange() {
const oldBvid = lastBvid;
const oldPage = lastPage;
// 重置状态
BN.state.reset();
// 如果面板可见,检测 BVID 或分P 变化则自动刷新
if (BN.state.panelVisible && BN.subtitle) {
const newBvid = BN.subtitle.extractBvid(location.href);
const newPage = BN.subtitle.extractPageIndex(location.href);
if (newBvid && (newBvid !== oldBvid || newPage !== oldPage)) {
lastBvid = newBvid;
lastPage = newPage;
setTimeout(async () => {
if (BN.state.panelVisible) {
await BN.subtitle.refresh();
BN.panel.resetDocAuto();
BN.panel.renderDoc();
}
}, 1000);
}
}
}
// ── 启动 ──
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();