From 94879e42580f6a208362fccc376e29e75417c53a Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sat, 25 Sep 2021 11:22:14 +0200 Subject: [PATCH] Fix target="_blank" links not opening, use new api for new window handling --- frontend/ts/index.ts | 1 + src/Application.ts | 30 ++++++++++++++++++++++++------ src/windows/MainWindow.ts | 8 +++----- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/frontend/ts/index.ts b/frontend/ts/index.ts index f3c3976..0db500e 100644 --- a/frontend/ts/index.ts +++ b/frontend/ts/index.ts @@ -443,6 +443,7 @@ function loadService(serviceId: number, service: FrontService) { view.setAttribute('enableRemoteModule', 'false'); view.setAttribute('partition', 'persist:service_' + service.partition); view.setAttribute('autosize', 'true'); + view.setAttribute('allowpopups', 'true'); if (specialPages) view.setAttribute('src', specialPages.empty); // Error handling diff --git a/src/Application.ts b/src/Application.ts index 0104b96..ab58926 100644 --- a/src/Application.ts +++ b/src/Application.ts @@ -1,4 +1,4 @@ -import {app, Menu, shell, Tray} from "electron"; +import {app, dialog, Menu, shell, Tray} from "electron"; import Meta from "./Meta"; import Config from "./Config"; import Updater from "./Updater"; @@ -52,16 +52,34 @@ export default class Application { return this.devMode; } + public async openExternalLink(url: string): Promise { + if (url.startsWith('https://')) { + console.log('Opening link', url); + await shell.openExternal(url); + } else { + const {response} = await dialog.showMessageBox({ + message: 'Are you sure you want to open this link?\n' + url, + type: 'question', + buttons: ['Cancel', 'Open link'], + }); + + if (response === 1) { + console.log('Opening link', url); + await shell.openExternal(url); + } + } + } + private setupElectronTweaks() { // Open external links in default OS browser app.on('web-contents-created', (e, contents) => { if (contents.getType() === 'webview') { console.log('Setting external links to open in default OS browser'); - contents.on('new-window', (e, url) => { - e.preventDefault(); - if (url.startsWith('https://')) { - shell.openExternal(url).catch(console.error); - } + contents.setWindowOpenHandler(details => { + const url = details.url; + this.openExternalLink(url) + .catch(console.error); + return {action: 'deny'}; }); } }); diff --git a/src/windows/MainWindow.ts b/src/windows/MainWindow.ts index aa98b67..eaed5e0 100644 --- a/src/windows/MainWindow.ts +++ b/src/windows/MainWindow.ts @@ -1,5 +1,5 @@ import path from "path"; -import {clipboard, ContextMenuParams, dialog, ipcMain, Menu, MenuItem, session, shell, webContents,} from "electron"; +import {clipboard, ContextMenuParams, dialog, ipcMain, Menu, MenuItem, session, webContents,} from "electron"; import ServiceSettingsWindow from "./ServiceSettingsWindow"; import SettingsWindow from "./SettingsWindow"; import Application from "../Application"; @@ -339,10 +339,8 @@ export default class MainWindow extends Window { menu.append(new MenuItem({ label: 'Open URL in default browser', click: () => { - if (props.linkURL.startsWith('https://')) { - shell.openExternal(props.linkURL) - .catch(console.error); - } + this.application.openExternalLink(props.linkURL) + .catch(console.error); }, })); }