Fix target="_blank" links not opening, use new api for new window handling

This commit is contained in:
Alice Gaudon 2021-09-25 11:22:14 +02:00
parent fd6fe7675a
commit 94879e4258
3 changed files with 28 additions and 11 deletions

View File

@ -443,6 +443,7 @@ function loadService(serviceId: number, service: FrontService) {
view.setAttribute('enableRemoteModule', 'false'); view.setAttribute('enableRemoteModule', 'false');
view.setAttribute('partition', 'persist:service_' + service.partition); view.setAttribute('partition', 'persist:service_' + service.partition);
view.setAttribute('autosize', 'true'); view.setAttribute('autosize', 'true');
view.setAttribute('allowpopups', 'true');
if (specialPages) view.setAttribute('src', specialPages.empty); if (specialPages) view.setAttribute('src', specialPages.empty);
// Error handling // Error handling

View File

@ -1,4 +1,4 @@
import {app, Menu, shell, Tray} from "electron"; import {app, dialog, Menu, shell, Tray} from "electron";
import Meta from "./Meta"; import Meta from "./Meta";
import Config from "./Config"; import Config from "./Config";
import Updater from "./Updater"; import Updater from "./Updater";
@ -52,16 +52,34 @@ export default class Application {
return this.devMode; return this.devMode;
} }
public async openExternalLink(url: string): Promise<void> {
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() { private setupElectronTweaks() {
// Open external links in default OS browser // Open external links in default OS browser
app.on('web-contents-created', (e, contents) => { app.on('web-contents-created', (e, contents) => {
if (contents.getType() === 'webview') { if (contents.getType() === 'webview') {
console.log('Setting external links to open in default OS browser'); console.log('Setting external links to open in default OS browser');
contents.on('new-window', (e, url) => { contents.setWindowOpenHandler(details => {
e.preventDefault(); const url = details.url;
if (url.startsWith('https://')) { this.openExternalLink(url)
shell.openExternal(url).catch(console.error); .catch(console.error);
} return {action: 'deny'};
}); });
} }
}); });

View File

@ -1,5 +1,5 @@
import path from "path"; 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 ServiceSettingsWindow from "./ServiceSettingsWindow";
import SettingsWindow from "./SettingsWindow"; import SettingsWindow from "./SettingsWindow";
import Application from "../Application"; import Application from "../Application";
@ -339,10 +339,8 @@ export default class MainWindow extends Window {
menu.append(new MenuItem({ menu.append(new MenuItem({
label: 'Open URL in default browser', label: 'Open URL in default browser',
click: () => { click: () => {
if (props.linkURL.startsWith('https://')) { this.application.openExternalLink(props.linkURL)
shell.openExternal(props.linkURL)
.catch(console.error); .catch(console.error);
}
}, },
})); }));
} }