From b9b5a621051d6127e667e5568ada98689331b0ba Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Sat, 25 Feb 2023 17:42:26 -0500 Subject: [PATCH] Fix electron link handling (#396) --- electron/gameWindow.js | 43 +++++++++--------------------------------- electron/main.js | 3 +++ electron/menu.js | 2 +- electron/utils.js | 3 +++ 4 files changed, 16 insertions(+), 35 deletions(-) diff --git a/electron/gameWindow.js b/electron/gameWindow.js index fe4abc47f..e114a6b1d 100644 --- a/electron/gameWindow.js +++ b/electron/gameWindow.js @@ -1,11 +1,10 @@ /* eslint-disable @typescript-eslint/no-var-requires */ -const { app, BrowserWindow } = require("electron"); +const { app, shell, BrowserWindow } = require("electron"); const log = require("electron-log"); const utils = require("./utils"); const achievements = require("./achievements"); const menu = require("./menu"); const api = require("./api-server"); -const cp = require("child_process"); const path = require("path"); const { windowTracker } = require("./windowTracker"); @@ -48,39 +47,15 @@ async function createWindow(killall) { window.show(); if (debug) window.webContents.openDevTools(); - window.webContents.on("new-window", async function (e, url) { - // Let's make sure sure we have a proper url - let parsedUrl; - try { - parsedUrl = new URL(url); - } catch (_) { - // This is an invalid url, let's just do nothing - log.warn(`Invalid url found: ${url}`); - e.preventDefault(); - return; - } - - // Just use the default handling for file requests, they should be intercepted in main.js file protocol intercept. - if (url.startswith("file://")) return; - - if (process.platform === "win32") { - // If we have parameters in the URL, explorer.exe won't register the URL and will open the file explorer instead. - let urlToOpen = parsedUrl.toString(); - if (parsedUrl.search) { - log.log(`Cannot open a path with parameters: ${parsedUrl.search}`); - urlToOpen = urlToOpen.replace(parsedUrl.search, ""); - // It would be possible to launch an URL with parameter using this, but it would mess up the process again... - // const escapedUri = parsedUrl.href.replace('&', '^&'); - // cp.spawn("cmd.exe", ["/c", "start", escapedUri], { detached: true, stdio: "ignore" }); - } - - cp.spawn("explorer", [urlToOpen], { detached: true, stdio: "ignore" }); - } else { - // and open every other protocols on the browser - utils.openExternal(url); - } - e.preventDefault(); + window.webContents.setWindowOpenHandler(({ url }) => { + // File protocol is allowed because it will use the file protocol intercept from main.js + if (url.startsWith("file://")) return { action: "allow" }; + // Only http and https requests will be forwarded to browser. + // By using shell.openExternal and returning action: "deny" + if (url.startsWith("http://") || url.startsWith("https://")) shell.openExternal(url); + return { action: "deny" }; }); + window.webContents.backgroundThrottling = false; achievements.enableAchievementsInterval(window); diff --git a/electron/main.js b/electron/main.js index 3dd660a93..4c9477dfe 100644 --- a/electron/main.js +++ b/electron/main.js @@ -72,6 +72,9 @@ function setStopProcessHandler(app, window) { log.error(error); } + // This may no longer be needed due to the return of action: deny in gameWindow.js setWindowOpenHandler. + // Nothing currently sets the app_playerOpenedExternalLink flag. See utils.js openExternal comment for more info. + // ------- // Because of a steam limitation, if the player has launched an external browser, // steam will keep displaying the game as "Running" in their UI as long as the browser is up. // So we'll alert the player to close their browser. diff --git a/electron/menu.js b/electron/menu.js index c7449efd8..f7ebc8bbc 100644 --- a/electron/menu.js +++ b/electron/menu.js @@ -310,7 +310,7 @@ function getMenu(window) { }) .then(({ response }) => { if (response === 1) { - utils.openExternal("https://github.com/bitburner-official/bitburner-vscode"); + shell.openExternal("https://github.com/bitburner-official/bitburner-vscode"); } }); }, diff --git a/electron/utils.js b/electron/utils.js index 12c93419c..3913151f5 100644 --- a/electron/utils.js +++ b/electron/utils.js @@ -87,6 +87,9 @@ async function writeToast(window, message, type = "info", duration = 2000) { await window.webContents.executeJavaScript(`window.appNotifier.toast("${message}", "${type}", ${duration});`, true); } +// This may no longer be needed due to the return of { action: "deny" } in gameWindow.js setWindowOpenHandler. +// Currently this is unused so that this can be tested. If the issue no longer exists, this util will be removed. +// Otherwise, its use will be reimplemented. function openExternal(url) { shell.openExternal(url); global.app_playerOpenedExternalLink = true;