-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
196 lines (167 loc) · 5.29 KB
/
Copy pathbackground.js
File metadata and controls
196 lines (167 loc) · 5.29 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// QuickTab - Background Service Worker
// 管理最近标签列表和消息通信
const MAX_TABS = 10;
const STORAGE_KEY = 'lastTabs';
// ========== 工具函数 ==========
// Serial queue to prevent read-modify-write race conditions on storage
let storageQueue = Promise.resolve();
function withTabList(fn) {
storageQueue = storageQueue
.then(async () => {
const result = await chrome.storage.session.get(STORAGE_KEY);
const tabList = result[STORAGE_KEY] || [];
const newList = await fn(tabList);
if (newList !== undefined) {
await chrome.storage.session.set({ [STORAGE_KEY]: newList });
}
})
.catch((error) => {
console.error('QuickTab: Storage operation error', error);
});
return storageQueue;
}
// 获取标签列表(只读,不需要串行)
async function getTabList() {
const result = await chrome.storage.session.get(STORAGE_KEY);
return result[STORAGE_KEY] || [];
}
// 检查 URL 是否可注入 content script
function isInjectableUrl(url) {
if (!url) return false;
return !url.startsWith('chrome://') &&
!url.startsWith('chrome-extension://') &&
!url.startsWith('edge://') &&
!url.startsWith('about:') &&
!url.startsWith('https://chrome.google.com/webstore');
}
// ========== 标签列表管理 ==========
// 记录标签激活
async function recordTabActivation(tabId) {
let tab;
try {
tab = await chrome.tabs.get(tabId);
} catch {
return;
}
if (!tab || !tab.url) return;
await withTabList((tabList) => {
// 查找是否已存在
const existingIndex = tabList.findIndex(t => t.id === tabId);
if (existingIndex !== -1) {
tabList.splice(existingIndex, 1);
}
tabList.unshift({
id: tab.id,
title: tab.title || 'Untitled',
url: tab.url || '',
favIconUrl: tab.favIconUrl || ''
});
if (tabList.length > MAX_TABS) {
tabList.pop();
}
return tabList;
});
}
// 移除关闭的标签
async function removeTab(tabId) {
await withTabList((tabList) => {
return tabList.filter(t => t.id !== tabId);
});
}
// 更新标签信息(URL 变化等)
async function updateTabInfo(tabId, changeInfo, tab) {
if (changeInfo.status !== 'complete') return;
let shouldRecord = false;
await withTabList((tabList) => {
const tabInfo = tabList.find((t) => t.id === tabId);
if (tabInfo) {
tabInfo.title = tab.title || tabInfo.title;
tabInfo.url = tab.url || tabInfo.url;
tabInfo.favIconUrl = tab.favIconUrl || tabInfo.favIconUrl;
return tabList;
}
shouldRecord = Boolean(tab.url && tab.active);
return undefined;
});
if (shouldRecord) {
await recordTabActivation(tabId);
}
}
// ========== 事件监听 ==========
// 标签激活
chrome.tabs.onActivated.addListener(async (activeInfo) => {
await recordTabActivation(activeInfo.tabId);
});
// 标签关闭
chrome.tabs.onRemoved.addListener(async (tabId) => {
await removeTab(tabId);
});
// 标签更新
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
await updateTabInfo(tabId, changeInfo, tab);
});
// ========== 快捷键与消息 ==========
// 快捷键触发
chrome.commands.onCommand.addListener(async (command) => {
if (command === 'toggle-panel') {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab && isInjectableUrl(tab.url)) {
await chrome.tabs.sendMessage(tab.id, { action: 'togglePanel' });
}
} catch {
// Content script not loaded on this page (e.g., freshly installed, not yet refreshed)
}
}
});
// 消息处理
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message.action === 'getTabList') {
(async () => {
const tabs = await getTabList();
const settings = await chrome.storage.local.get('showNonInjectableTabs');
const showAll = settings.showNonInjectableTabs || false;
const filtered = showAll ? tabs : tabs.filter(t => isInjectableUrl(t.url));
sendResponse({ tabs: filtered });
})();
return true; // 异步响应
}
if (message.action === 'switchToTab') {
(async () => {
const tab = await chrome.tabs.get(message.tabId);
await chrome.tabs.update(message.tabId, { active: true });
await chrome.windows.update(tab.windowId, { focused: true });
sendResponse({ success: true });
})().catch((error) => {
sendResponse({ success: false, error: error.message });
});
return true;
}
});
// 初始化:记录所有标签页
async function initAllTabs() {
try {
const existing = await chrome.storage.session.get(STORAGE_KEY);
if (Array.isArray(existing[STORAGE_KEY])) {
return;
}
const allTabs = await chrome.tabs.query({});
const tabList = [];
// 按最后访问时间排序(如果有的话)
allTabs.sort((a, b) => (b.lastAccessed || 0) - (a.lastAccessed || 0));
for (const tab of allTabs) {
if (tab.url && tabList.length < MAX_TABS) {
tabList.push({
id: tab.id,
title: tab.title || 'Untitled',
url: tab.url || '',
favIconUrl: tab.favIconUrl || ''
});
}
}
await chrome.storage.session.set({ [STORAGE_KEY]: tabList });
} catch (error) {
console.error('QuickTab: Init error', error);
}
}
initAllTabs();