-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
124 lines (106 loc) · 3.49 KB
/
Copy pathbackground.js
File metadata and controls
124 lines (106 loc) · 3.49 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
// 背景服务模块(保持持久状态)
const BackgroundService = (() => {
// tabId -> { targetUrl, lastUrl }
const tabData = new Map();
let currentActiveTabId = null;
// 页面开始导航
const handleBeforeNavigate = (details) => {
if (details.frameId === 0) {
tabData.set(details.tabId, {
targetUrl: details.url,
lastUrl: details.url
});
if (details.tabId === currentActiveTabId) {
console.log('[导航开始]', details.url);
}
}
};
// URL 确定(包括 hash / SPA)
const handleCommitted = (details) => {
if (details.frameId === 0 && tabData.has(details.tabId)) {
const data = tabData.get(details.tabId);
data.lastUrl = details.url;
if (details.tabId === currentActiveTabId) {
console.log('[URL变化]', details.url);
}
}
};
// 激活标签页
const handleTabActivated = ({ tabId }) => {
currentActiveTabId = tabId;
chrome.tabs.get(tabId, (tab) => {
const data = tabData.get(tabId) || {};
const displayUrl = data.targetUrl || tab.url;
if (displayUrl && !displayUrl.startsWith('chrome://')) {
console.log('[切换标签页]', displayUrl);
}
});
};
// 实时监听 URL 更新
const handleTabUpdated = (tabId, changeInfo) => {
if (tabId === currentActiveTabId && changeInfo.url) {
const data = tabData.get(tabId) || {};
data.lastUrl = changeInfo.url;
tabData.set(tabId, data);
console.log('[实时更新]', changeInfo.url);
}
};
// 标签页关闭
const handleTabRemoved = (tabId) => {
tabData.delete(tabId);
};
// 前端路由(SPA)变化通知
const handleSpaUrlChange = (msg, sender) => {
if (msg.url && sender.tab?.id) {
const data = tabData.get(sender.tab.id) || {};
data.lastUrl = msg.url;
tabData.set(sender.tab.id, data);
if (sender.tab.id === currentActiveTabId) {
console.log('[前端路由变化]', msg.url);
}
}
};
// 响应 popup 请求 URL
const handlePopupRequest = (sendResponse) => {
const data = tabData.get(currentActiveTabId);
const url = data?.lastUrl || '';
console.log('[popup 请求当前 URL]', url);
sendResponse({ url });
};
// 通用消息分发器(单一监听器 ✅ 避免连接错误)
const handleMessage = (message, sender, sendResponse) => {
if (message.type === 'url_change') {
handleSpaUrlChange(message, sender);
return false; // 非异步
}
if (message.type === 'get_current_url') {
handlePopupRequest(sendResponse);
return true; // 异步响应
}
return false; // 忽略其它类型
};
// 初始化服务
const initialize = () => {
chrome.webNavigation.onBeforeNavigate.addListener(handleBeforeNavigate);
chrome.webNavigation.onCommitted.addListener(handleCommitted);
chrome.tabs.onActivated.addListener(handleTabActivated);
chrome.tabs.onUpdated.addListener(handleTabUpdated);
chrome.tabs.onRemoved.addListener(handleTabRemoved);
chrome.runtime.onMessage.addListener(handleMessage);
chrome.runtime.onInstalled.addListener((details) => {
console.log('扩展已安装或更新', details);
});
chrome.runtime.onConnect.addListener((port) => {
if (port.name === 'popup') {
console.log('popup 页面已打开');
port.onDisconnect.addListener(() => {
console.log('popup 页面已关闭');
});
}
});
};
return {
initialize
};
})();
BackgroundService.initialize();