From 9c6bdae556fea842432ed46c17985f083d7186fb Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Mon, 12 Sep 2022 17:44:35 -0700 Subject: [PATCH] cmd/tsconnect: use the parent window for `beforeunload` event listener The SSH session may be rendered in a different window that the one that is executing the script. Signed-off-by: Mihai Parparita --- cmd/tsconnect/src/lib/ssh.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cmd/tsconnect/src/lib/ssh.ts b/cmd/tsconnect/src/lib/ssh.ts index f6dc62aac..defd9b739 100644 --- a/cmd/tsconnect/src/lib/ssh.ts +++ b/cmd/tsconnect/src/lib/ssh.ts @@ -14,6 +14,7 @@ export function runSSHSession( onDone: () => void, terminalOptions?: ITerminalOptions ) { + const parentWindow = termContainerNode.ownerDocument.defaultView ?? window const term = new Terminal({ cursorBlink: true, allowProposedApi: true, @@ -57,22 +58,19 @@ export function runSSHSession( resizeObserver?.disconnect() term.dispose() if (handleBeforeUnload) { - window.removeEventListener("beforeunload", handleBeforeUnload) + parentWindow.removeEventListener("beforeunload", handleBeforeUnload) } onDone() }, }) // Make terminal and SSH session track the size of the containing DOM node. - resizeObserver = - new termContainerNode.ownerDocument.defaultView!.ResizeObserver(() => - fitAddon.fit() - ) + resizeObserver = new parentWindow.ResizeObserver(() => fitAddon.fit()) resizeObserver.observe(termContainerNode) term.onResize(({ rows, cols }) => sshSession.resize(rows, cols)) // Close the session if the user closes the window without an explicit // exit. handleBeforeUnload = () => sshSession.close() - window.addEventListener("beforeunload", handleBeforeUnload) + parentWindow.addEventListener("beforeunload", handleBeforeUnload) }