Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
259 lines (228 loc) · 7.01 KB
/
Copy pathbackground.js
File metadata and controls
259 lines (228 loc) · 7.01 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Better Tab Split - Background Service Worker
// 管理扩展状态和处理链接拦截
// 存储每个 tab 的启用状态
const enabledTabs = new Map();
// 自动激活设置(缓存)
let autoActivate = true;
// 初始化:从 storage 读取设置
chrome.storage.sync.get({ autoActivate: true }, (items) => {
autoActivate = items.autoActivate;
});
// 监听设置变化
chrome.storage.onChanged.addListener((changes, area) => {
if (area === 'sync' && changes.autoActivate) {
autoActivate = changes.autoActivate.newValue;
}
});
// 图标路径
const ICONS = {
disabled: {
16: 'icons/icon-disabled-16.png',
32: 'icons/icon-disabled-32.png',
48: 'icons/icon-disabled-48.png',
128: 'icons/icon-disabled-128.png'
},
off: {
16: 'icons/icon-off-16.png',
32: 'icons/icon-off-32.png',
48: 'icons/icon-off-48.png',
128: 'icons/icon-off-128.png'
},
on: {
16: 'icons/icon-on-16.png',
32: 'icons/icon-on-32.png',
48: 'icons/icon-on-48.png',
128: 'icons/icon-on-128.png'
}
};
// 检查 tab 是否在 Split View 中
async function isInSplitView(tabId) {
try {
const tab = await chrome.tabs.get(tabId);
// splitViewId 存在且不等于 -1 (SPLIT_VIEW_ID_NONE) 表示在 Split View 中
return tab.splitViewId !== undefined && tab.splitViewId !== chrome.tabs.SPLIT_VIEW_ID_NONE;
} catch (e) {
console.error('Error checking split view status:', e);
return false;
}
}
// 获取同一 Split View 中的另一个 tab
async function getOtherSplitViewTab(tabId) {
try {
const currentTab = await chrome.tabs.get(tabId);
if (!currentTab.splitViewId || currentTab.splitViewId === chrome.tabs.SPLIT_VIEW_ID_NONE) {
return null;
}
// 查询同一窗口中具有相同 splitViewId 的所有 tabs
const tabs = await chrome.tabs.query({
windowId: currentTab.windowId
});
// 找到同一 Split View 中的另一个 tab
const otherTab = tabs.find(t =>
t.id !== tabId &&
t.splitViewId === currentTab.splitViewId
);
return otherTab || null;
} catch (e) {
console.error('Error getting other split view tab:', e);
return null;
}
}
// 更新扩展图标和状态
async function updateExtensionState(tabId) {
const inSplitView = await isInSplitView(tabId);
if (!inSplitView) {
// 不在 Split View 中 - 灰色图标
await chrome.action.setIcon({ tabId, path: ICONS.disabled });
await chrome.action.setTitle({
tabId,
title: 'Better Tab Split (未启用 Split View)'
});
await chrome.action.disable(tabId);
enabledTabs.delete(tabId);
} else {
// 在 Split View 中 - 启用扩展
await chrome.action.enable(tabId);
// 如果 tab 没有被用户手动设置过状态,根据自动激活设置决定
if (!enabledTabs.has(tabId)) {
enabledTabs.set(tabId, autoActivate);
}
const isEnabled = enabledTabs.get(tabId);
if (isEnabled) {
await chrome.action.setIcon({ tabId, path: ICONS.on });
await chrome.action.setTitle({
tabId,
title: 'Better Tab Split: 开启 (点击关闭)'
});
// 注入 content script
await injectContentScript(tabId, true);
} else {
await chrome.action.setIcon({ tabId, path: ICONS.off });
await chrome.action.setTitle({
tabId,
title: 'Better Tab Split: 关闭 (点击开启)'
});
// 禁用 content script
await injectContentScript(tabId, false);
}
}
}
// 检查是否可以在该 URL 注入脚本
function canInjectScript(url) {
if (!url) return false;
// 不能在这些特殊页面注入脚本
const restrictedProtocols = [
'chrome://',
'chrome-extension://',
'edge://',
'about:',
'devtools://',
'view-source:',
'chrome-search://'
];
return !restrictedProtocols.some(protocol => url.startsWith(protocol));
}
// 注入或更新 content script
async function injectContentScript(tabId, enabled) {
// 先获取 tab 信息检查 URL
let tab;
try {
tab = await chrome.tabs.get(tabId);
} catch (e) {
return; // tab 不存在,静默返回
}
if (!canInjectScript(tab.url)) {
// 无法在此页面注入脚本,静默跳过
return;
}
// 先尝试发送消息给已存在的 content script
try {
await chrome.tabs.sendMessage(tabId, {
type: 'UPDATE_STATE',
enabled: enabled
});
return; // 成功发送消息,不需要注入
} catch (e) {
// content script 不存在或无法通信,继续尝试注入
}
// 尝试注入 content script
try {
await chrome.scripting.executeScript({
target: { tabId },
files: ['content.js']
});
} catch (e) {
// 注入失败(可能是受限页面),静默返回
return;
}
// 发送初始状态
try {
await chrome.tabs.sendMessage(tabId, {
type: 'UPDATE_STATE',
enabled: enabled
});
} catch (e) {
// 发送消息失败,静默处理
}
}
// 处理扩展图标点击
chrome.action.onClicked.addListener(async (tab) => {
const inSplitView = await isInSplitView(tab.id);
if (!inSplitView) {
return; // 不在 Split View 中,不做任何操作
}
// 切换启用状态
const currentState = enabledTabs.get(tab.id) || false;
const newState = !currentState;
enabledTabs.set(tab.id, newState);
// 更新图标和 content script
await updateExtensionState(tab.id);
// 同时更新同一 Split View 中的另一个 tab
const otherTab = await getOtherSplitViewTab(tab.id);
if (otherTab) {
enabledTabs.set(otherTab.id, newState);
await updateExtensionState(otherTab.id);
}
});
// 监听 tab 激活
chrome.tabs.onActivated.addListener(async (activeInfo) => {
await updateExtensionState(activeInfo.tabId);
});
// 监听 tab 更新
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete') {
await updateExtensionState(tabId);
}
});
// 监听 tab 关闭,清理状态
chrome.tabs.onRemoved.addListener((tabId) => {
enabledTabs.delete(tabId);
});
// 处理来自 content script 的消息
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'OPEN_IN_OTHER_SPLIT') {
handleOpenInOtherSplit(sender.tab.id, message.url);
sendResponse({ success: true });
} else if (message.type === 'CHECK_ENABLED') {
const isEnabled = enabledTabs.get(sender.tab.id) || false;
sendResponse({ enabled: isEnabled });
}
return true; // 保持消息通道开放
});
// 在另一个 Split View 中打开链接
async function handleOpenInOtherSplit(sourceTabId, url) {
try {
const otherTab = await getOtherSplitViewTab(sourceTabId);
if (otherTab) {
// 在另一个 split view tab 中打开链接
await chrome.tabs.update(otherTab.id, { url: url });
} else {
// 如果找不到另一个 split view tab,在新标签页中打开
await chrome.tabs.create({ url: url });
}
} catch (e) {
console.error('Error opening in other split:', e);
// 失败时在新标签页中打开
await chrome.tabs.create({ url: url });
}
}