(url: string, tabId?: number)
| 112 | |
| 113 | // 在当前页后打开一个新页面,如果指定tabId则在该tab后打开 |
| 114 | export async function openInCurrentTab(url: string, tabId?: number): Promise<chrome.tabs.Tab | undefined> { |
| 115 | const tab = await (tabId ? getTab(tabId) : getCurrentTab()); |
| 116 | const createProperties: chrome.tabs.CreateProperties = { url }; |
| 117 | if (tab) { |
| 118 | // 添加 openerTabId 有可能出现 Error "Tab opener must be in the same window as the updated tab." |
| 119 | if (tab.id! >= 0) { |
| 120 | // 如 Tab API 有提供 tab.id, 则指定 tab.id |
| 121 | createProperties.openerTabId = tab.id; |
| 122 | if (tab.windowId! >= 0) { |
| 123 | // 如 Tab API 有提供 tab.windowId, 则指定 tab.windowId |
| 124 | createProperties.windowId = tab.windowId; |
| 125 | } |
| 126 | } |
| 127 | createProperties.index = tab.index + 1; |
| 128 | } |
| 129 | // 先尝试以 openerTabId 和 windowId 打开 |
| 130 | try { |
| 131 | return await chrome.tabs.create(createProperties); |
| 132 | } catch (e: any) { |
| 133 | console.error("Error opening tab:", e); |
| 134 | } |
| 135 | // 失败的话,删去 openerTabId 和 windowId ,再次尝试打开 |
| 136 | delete createProperties.openerTabId; |
| 137 | delete createProperties.windowId; |
| 138 | try { |
| 139 | return await chrome.tabs.create(createProperties); |
| 140 | } catch (e: any) { |
| 141 | console.error("Retry opeing tab error:", e); |
| 142 | } |
| 143 | return undefined; |
| 144 | } |
| 145 | |
| 146 | // 检查 connect 是否改变,是否能够静默更新 |
| 147 | // 用于订阅本身的更新检查和普通脚本的静默更新检查 |
no test coverage detected