Skip to content

Commit

Permalink
fix: Discard page even if it's trying to prevent closing
Browse files Browse the repository at this point in the history
  • Loading branch information
mbnuqw committed Apr 5, 2024
1 parent 1311c1c commit 51ad480
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/defaults/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const DEFAULT_SETTINGS: SettingsState = {
newTabCtxReopen: false,
tabWarmupOnHover: true,
tabSwitchDelay: 0,
forceDiscard: true,

// New tab position
moveNewTabPin: 'start',
Expand Down
20 changes: 19 additions & 1 deletion src/services/tabs.fg.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,25 @@ export async function discardTabs(tabIds: ID[] = []): Promise<void> {
}
}

browser.tabs.discard(tabIds).catch(err => {
// Force discard for pages that prevent their own closing
if (Settings.state.forceDiscard && Permissions.allUrls) {
const forceDiscardInjection =
'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
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)
})
}
Expand Down
1 change: 1 addition & 0 deletions src/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface SettingsState {
newTabCtxReopen: boolean
tabWarmupOnHover: boolean
tabSwitchDelay: number
forceDiscard: boolean

// New tab position
moveNewTabPin: (typeof SETTINGS_OPTIONS.moveNewTabPin)[number]
Expand Down

0 comments on commit 51ad480

Please sign in to comment.