File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
193214export function errorMsg ( e : any ) : string {
You can’t perform that action at this time.
0 commit comments