Skip to content

Commit eb4d1ab

Browse files
authored
🎨 (MV2) 增强 chrome.tabs.create 兼容性 (#640)
1 parent 51f3d0c commit eb4d1ab

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

src/pkg/utils/utils.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,32 @@ export async function openInCurrentTab(url: string) {
183183
const createProperties: chrome.tabs.CreateProperties = { url };
184184
if (tab) {
185185
// 添加 openerTabId 有可能出现 Error "Tab opener must be in the same window as the updated tab."
186-
createProperties.openerTabId = tab.id;
187-
createProperties.windowId = tab.windowId;
186+
if (tab.id! >= 0) {
187+
// 如 Tab API 有提供 tab.id, 則指定 tab.id
188+
createProperties.openerTabId = tab.id;
189+
if (tab.windowId! >= 0) {
190+
// 如 Tab API 有提供 tab.windowId, 則指定 tab.windowId
191+
createProperties.windowId = tab.windowId;
192+
}
193+
}
188194
createProperties.index = tab.index + 1;
189195
}
190-
return chrome.tabs.create(createProperties);
196+
// 先嘗試以 openerTabId 和 windowId 打開
197+
try {
198+
await chrome.tabs.create(createProperties);
199+
return;
200+
} catch {
201+
// do nothing
202+
}
203+
// 失敗的話,刪去 openerTabId 和 windowId ,再次嘗試打開
204+
delete createProperties.openerTabId;
205+
delete createProperties.windowId;
206+
try {
207+
await chrome.tabs.create(createProperties);
208+
return;
209+
} catch {
210+
// do nothing
211+
}
191212
}
192213

193214
export function errorMsg(e: any): string {

0 commit comments

Comments
 (0)