-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
99 lines (84 loc) · 2.64 KB
/
Copy pathcontent.js
File metadata and controls
99 lines (84 loc) · 2.64 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
/**
* BiViNote Content Script
* 入口脚本 - 初始化所有模块,监听 SPA 路由变化
*/
(async function () {
'use strict';
const BN = window.BiViNote;
// ── 初始化 ──
async function init() {
try {
// 加载设置
await BN.settings.load();
// 延迟 2 秒初始化 UI,等待 B 站 Vue 应用渲染
setTimeout(() => {
// 创建嵌入面板
BN.panel.create();
// 自动显示面板
BN.panel.show();
// 根据设置显示悬浮功能条
if (BN.state.settings.showFloatToolbar !== false) {
BN.panel.showCollapse();
}
// 自动加载字幕
const currentBvid = BN.subtitle?.extractBvid(location.href) || '';
const currentPage = BN.subtitle?.extractPageIndex(location.href) || 1;
if (BN.subtitle && currentBvid) {
lastBvid = currentBvid;
lastPage = currentPage;
BN.subtitle.refresh();
}
console.log('[BiViNote] UI initialized');
}, 2000);
console.log('[BiViNote] Initialized successfully');
} catch (err) {
console.error('[BiViNote] Initialization failed:', err);
}
}
// ── SPA 路由监听 ──
// 恢复 MutationObserver 方式(B站 SPA 导航依赖此机制)
// 优化:节流回调,避免高频触发
let lastUrl = location.href;
let lastBvid = '';
let lastPage = 1;
let urlCheckTimer = null;
const urlObserver = new MutationObserver(() => {
if (urlCheckTimer) return;
urlCheckTimer = setTimeout(() => {
urlCheckTimer = null;
if (location.href !== lastUrl) {
lastUrl = location.href;
onRouteChange();
}
}, 300);
});
urlObserver.observe(document.body, { childList: true, subtree: true });
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();
}
})();