-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
97 lines (89 loc) · 2.84 KB
/
Copy pathbackground.js
File metadata and controls
97 lines (89 loc) · 2.84 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
/**
* Qoder Test Helper - Background Service Worker
* 消息中转、数据存储、标签页管理
*/
// 监听扩展消息
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.type) {
case 'OPEN_REPORT':
// 在新标签页打开报告
chrome.tabs.create({ url: message.url });
sendResponse({ ok: true });
break;
case 'GET_REPORT':
// 获取报告数据
chrome.storage.local.get(['selectorReports', 'recorderReports'], (result) => {
sendResponse({
selectorReports: result.selectorReports || [],
recorderReports: result.recorderReports || []
});
});
return true; // 异步响应
case 'DELETE_REPORT':
// 删除指定报告
const reportType = message.reportType;
const reportId = message.reportId;
chrome.storage.local.get([reportType], (result) => {
const reports = result[reportType] || [];
const filtered = reports.filter((_, i) => i !== reportId);
chrome.storage.local.set({ [reportType]: filtered }, () => {
sendResponse({ ok: true });
});
});
return true;
case 'CLEAR_ALL_DATA':
chrome.storage.local.clear(() => {
sendResponse({ ok: true });
});
return true;
case 'EXPORT_REPORT':
// 导出单个报告为JSON
const reportData = message.data;
const jsonStr = JSON.stringify(reportData, null, 2);
const blob = new Blob([jsonStr], { type: 'application/json' });
// 使用 download API
const reader = new FileReader();
reader.onload = function() {
chrome.downloads?.download({
url: reader.result,
filename: message.filename || `qoder-report-${Date.now()}.json`,
saveAs: true
});
};
reader.readAsDataURL(blob);
sendResponse({ ok: true });
break;
}
});
// 扩展安装时初始化
chrome.runtime.onInstalled.addListener(() => {
console.log('Qoder Test Helper 已安装');
// 初始化存储
chrome.storage.local.set({
selectorReports: [],
recorderReports: [],
settings: {
maxReports: 50,
autoClean: true
}
});
});
// 标签页更新时通知 content script
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete') {
// 可选:通知 content script 页面已刷新
chrome.tabs.sendMessage(tabId, { type: 'PAGE_LOADED' }).catch(() => {
// content script 可能未注入,忽略错误
});
}
});
// 键盘快捷键支持(可选)
chrome.commands?.onCommand?.addListener((command) => {
if (command === 'toggle-selector') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
chrome.tabs.sendMessage(tabs[0].id, { type: 'TOGGLE_SELECTOR', active: true });
}
});
}
});