Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: beforeunload not getting dispatched #263

Merged
merged 3 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading