Skip to content

Commit

Permalink
fix: tab discard regression 51ad480
Browse files Browse the repository at this point in the history
  • Loading branch information
mbnuqw committed Apr 6, 2024
1 parent 7f79dc9 commit 18edf32
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/services/tabs.fg.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,27 +1135,36 @@ export async function discardTabs(tabIds: ID[] = []): Promise<void> {
}
}

// Force discard for pages that prevent their own closing
if (Settings.state.forceDiscard && Permissions.allUrls) {
// Try to discard tabs
await browser.tabs.discard(tabIds).catch(err => {
Logs.err('Tabs.discardTabs: Cannot discard:', err)
})

// Find not discarded tabs that might prevent their closing
const secondTryIds = tabIds.filter(id => {
const tab = Tabs.byId[id]
return tab && !tab.discarded && tab.url.startsWith('h')
})

// Try to reset closing prevention and discard such tabs
if (Settings.state.forceDiscard && Permissions.allUrls && secondTryIds.length) {
const forceDiscardInjection =
'window.onbeforeunload=null;window.addEventListener("beforeunload", e => {e.returnValue = ""})'
'window.onbeforeunload=null;window.addEventListener("beforeunload", e => {e.returnValue=""})'
await Promise.allSettled(
tabIds.map(id => {
const tab = Tabs.byId[id]
if (!tab?.url.startsWith('h')) return
secondTryIds.map(id => {
return browser.tabs.executeScript(id, {
code: forceDiscardInjection,
runAt: 'document_start',
allFrames: true,
})
})
)
}

// Try to discard tabs
return browser.tabs.discard(tabIds).catch(err => {
Logs.err('Tabs.discardTabs: Cannot discard:', err)
})
// Second try
await browser.tabs.discard(secondTryIds).catch(err => {
Logs.err('Tabs.discardTabs: Cannot discard (second try):', err)
})
}
}

/**
Expand Down

0 comments on commit 18edf32

Please sign in to comment.