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('partition', 'persist:service_' + service.partition);
view.setAttribute('autosize', 'true');
view.setAttribute('allowpopups', 'true');
if (specialPages) view.setAttribute('src', specialPages.empty);
// 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 Config from "./Config";
import Updater from "./Updater";
@ -52,16 +52,34 @@ export default class Application {
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() {
// 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'};
});
}
});

View File

@ -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)
this.application.openExternalLink(props.linkURL)
.catch(console.error);
}
},
}));
}