Skip to content

Commit

Permalink
Merge pull request #263 from ioj4/main
Browse files Browse the repository at this point in the history
fix: beforeunload not getting dispatched
  • Loading branch information
SpikeHD authored Oct 6, 2024
2 parents 966850b + d1c1cd7 commit 2b6f798
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions src-tauri/injection/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare global {
emit: (event: string, payload: unknown) => void
TauriEvent: {
WINDOW_RESIZED: string
WINDOW_CLOSE_REQUESTED: string
}
}
shell: {
Expand Down
15 changes: 14 additions & 1 deletion src-tauri/injection/preinject.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { badPostMessagePatch, createLocalStorage, proxyFetch, proxyXHR } from './shared/recreate'
import { badPostMessagePatch, createLocalStorage, proxyFetch, proxyXHR, proxyAddEventListener } from './shared/recreate'
import { safemodeTimer, typingAnim } from './shared/ui'
import { cssSanitize, fetchImage, isJson, waitForApp, waitForElm, saferEval } from './shared/util'
import { applyNotificationCount } from './shared/window'
Expand Down Expand Up @@ -40,6 +40,7 @@ if (!window.__DORION_INITIALIZED__) window.__DORION_INITIALIZED__ = false
createLocalStorage()
proxyFetch()
proxyXHR()
proxyAddEventListener()

while (!window.__TAURI__) {
console.log('Waiting for definition...')
Expand Down Expand Up @@ -105,6 +106,18 @@ async function init() {

typingAnim()

// Discord Web depends on the `beforeunload` event being dispatched by the browser when
// a tab is closed. However, this event is not triggered by the Webview so we need to
// dispatch the `beforeunload` event ourselves.
const dispatchBeforeUnload = () => {
const event = new Event('beforeunload') as Event & { isTrustedOverwrite: boolean }
event.isTrustedOverwrite = true
window.dispatchEvent(event)
}

event.listen('beforeunload', dispatchBeforeUnload)
event.listen(event.TauriEvent.WINDOW_CLOSE_REQUESTED, dispatchBeforeUnload)

// Start the loading_log event listener
const logUnlisten = await event.listen('loading_log', (event: TauriEvent) => {
const log = event.payload as string
Expand Down
28 changes: 28 additions & 0 deletions src-tauri/injection/shared/recreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ export function proxyXHR() {
}
}

export function proxyAddEventListener() {
const original = window.addEventListener

window.addEventListener = function(...args: Parameters<typeof window.addEventListener>) {
const [type, listener] = args
if (type === 'beforeunload') {
args[1] = (...listenerArgs: Parameters<EventListener>) => {
// @ts-expect-error this is fine
const isTrustedOverwrite = listenerArgs[0]?.isTrustedOverwrite

if (isTrustedOverwrite !== undefined) {
const event = listenerArgs[0]
listenerArgs[0] = new Proxy(event, {
get(target, prop, receiver) {
if (prop === 'isTrusted') return isTrustedOverwrite
return Reflect.get(target, prop, receiver)
}
})
}

return ('handleEvent' in listener) ? listener.handleEvent(...listenerArgs) : listener(...listenerArgs)
}
}

return original(...args)
}
}

export function createLocalStorage() {
const iframe = document.createElement('iframe')

Expand Down
8 changes: 7 additions & 1 deletion src-tauri/src/functionality/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use tauri::{
image::Image,
menu::{MenuBuilder, MenuItemBuilder},
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
AppHandle, Manager,
AppHandle, Emitter, Manager,
};

use crate::{log, util::window_helpers::ultrashow};
Expand Down Expand Up @@ -60,6 +60,9 @@ pub fn create_tray(app: &AppHandle) -> Result<(), tauri::Error> {
.menu(&menu)
.on_menu_event(move |app, event| match event.id().as_ref() {
"quit" => {
if let Some(win) = app.get_webview_window("main") {
win.emit("beforeunload", ()).unwrap_or_default();
}
app.exit(0);
}
"open" => {
Expand All @@ -68,6 +71,9 @@ pub fn create_tray(app: &AppHandle) -> Result<(), tauri::Error> {
}
}
"restart" => {
if let Some(win) = app.get_webview_window("main") {
win.emit("beforeunload", ()).unwrap_or_default();
}
app.restart();
}
"reload" => {
Expand Down

0 comments on commit 2b6f798

Please sign in to comment.