-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
41 lines (36 loc) · 1.4 KB
/
background.js
File metadata and controls
41 lines (36 loc) · 1.4 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
// 监听安装事件
chrome.runtime.onInstalled.addListener(function() {
console.log('TOEFL Reading Practice扩展已安装');
});
// 添加错误监听器
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === 'reportError') {
console.error('扩展错误:', request.error);
sendResponse({received: true});
}
if (request.action === 'debugLog') {
console.log('扩展调试:', request.message);
sendResponse({received: true});
}
return true;
});
// 监听标签页更新,确保内容脚本已注入
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status === 'complete' && tab.url && tab.url.indexOf('chrome://') !== 0) {
// 仅在非Chrome内部页面上执行
console.log('标签页已加载完成:', tab.url);
// 为每个新加载的非Chrome页面注入内容脚本
// 这里不需要实际注入,因为manifest.json中已经配置了内容脚本
// 但我们可以发送消息通知内容脚本页面已加载完成
try {
chrome.tabs.sendMessage(tabId, { action: 'pageReady' }, function(response) {
// 忽略无响应错误
if (chrome.runtime.lastError) {
console.log('内容脚本可能未加载:', chrome.runtime.lastError.message);
}
});
} catch (error) {
console.error('发送消息错误:', error);
}
}
});