From 8a4ff84e958b3f434bc39be592b15244c4dc16f8 Mon Sep 17 00:00:00 2001 From: sawka Date: Sat, 16 Sep 2023 22:58:28 -0700 Subject: [PATCH] open all iframe (name=webview) links in new window. deny any other iframe navigation. --- src/emain.ts | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/emain.ts b/src/emain.ts index 0378b0cbe..bdf9e179d 100644 --- a/src/emain.ts +++ b/src/emain.ts @@ -169,10 +169,27 @@ function getMods(input: any) { } function shNavHandler(event: any, url: any) { - console.log("navigation", url); + console.log("navigation canceled", url); event.preventDefault(); } +function shFrameNavHandler(event: any, url: any) { + if (!event.frame || event.frame.parent == null) { + // only use this handler to process iframe events (non-iframe events go to shNavHandler) + return; + } + event.preventDefault(); + console.log(`frame-navigation url=${url} frame=${event.frame.name}`); + if (event.frame.name == "webview") { + // "webview" links always open in new window + // this will *not* effect the initial load because srcdoc does not count as an electron navigation + electron.shell.openExternal(url); + return; + } + console.log("frame navigation canceled"); + return; +} + function createMainWindow(clientData) { let bounds = calcBounds(clientData); let win = new electron.BrowserWindow({ @@ -266,6 +283,7 @@ function createMainWindow(clientData) { } }); win.webContents.on("will-navigate", shNavHandler); + win.webContents.on("will-frame-navigate", shFrameNavHandler); win.on( "resized", debounce(400, (e) => mainResizeHandler(e, win)) @@ -281,7 +299,7 @@ function createMainWindow(clientData) { win.on("close", () => { MainWindow = null; }); - win.webContents.setWindowOpenHandler(({url, frameName}) => { + win.webContents.setWindowOpenHandler(({ url, frameName }) => { if (url.startsWith("https://docs.getprompt.dev/")) { electron.shell.openExternal(url); } else if (url.startsWith("https://discord.gg/")) { @@ -292,7 +310,8 @@ function createMainWindow(clientData) { let newUrl = decodeURIComponent(param); electron.shell.openExternal(newUrl); } - return { action: 'deny' }; + console.log("window-open denied", url); + return { action: "deny" }; }); return win; }