-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
63 lines (58 loc) · 1.95 KB
/
Copy pathbackground.js
File metadata and controls
63 lines (58 loc) · 1.95 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
// 存储窗口ID
let windowId = null;
// 添加标签页状态跟踪
const tabStates = {};
// 监听插件图标点击事件
chrome.action.onClicked.addListener(async (tab) => {
// 检查content.js是否已注入
try {
// 先尝试发送ping消息
await new Promise((resolve, reject) => {
chrome.tabs.sendMessage(tab.id, {action: 'ping'}, (response) => {
if (chrome.runtime.lastError || !response) {
reject();
} else {
resolve();
}
});
});
} catch {
// 未注入则注入脚本和样式
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
});
await chrome.scripting.insertCSS({
target: { tabId: tab.id },
files: ['floating.css']
});
tabStates[tab.id] = true;
}
// 确保脚本加载完成后发送消息
// 发送显示窗口消息并处理错误
chrome.tabs.sendMessage(tab.id, { action: 'toggleXPathAnalyzer' }, (response) => {
if (chrome.runtime.lastError) {
console.error('发送消息失败:', chrome.runtime.lastError);
// 失败时尝试重新注入脚本
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['content.js']
}).then(() => {
chrome.tabs.sendMessage(tab.id, { action: 'toggleXPathAnalyzer' });
});
}
});
});
// 添加窗口关闭消息处理
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'windowClosed' && sender.tab) {
tabStates[sender.tab.id] = false;
sendResponse({ success: true });
}
});
// 监听窗口关闭事件
chrome.windows.onRemoved.addListener((removedWindowId) => {
if (removedWindowId === windowId) {
windowId = null;
}
});