Skip to content

Commit

Permalink
Open external links in system browser, closes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Sep 1, 2021
1 parent 4ee4434 commit d19ffec
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ const cp = require("child_process");
const portfinder = require("portfinder");
const prompt = require("electron-prompt");
const fs = require("fs");
const url = require("url");
const util = require("util");

const execFile = util.promisify(cp.execFile);
const mkdir = util.promisify(fs.mkdir);

function postConfigure(mainWindow) {
mainWindow.webContents.on("will-navigate", function (event, reqUrl) {
let requestedHost = new url.URL(reqUrl).host;
let currentHost = new url.URL(mainWindow.webContents.getURL()).host;
if (requestedHost && requestedHost != currentHost) {
event.preventDefault();
shell.openExternal(reqUrl);
}
});
}

class DatasetteServer {
constructor(app, port) {
this.app = app;
Expand Down Expand Up @@ -126,6 +138,7 @@ function createWindow() {
mainWindow.once("ready-to-show", () => {
mainWindow.show();
});
postConfigure(mainWindow);

portfinder.getPort(
{
Expand Down Expand Up @@ -180,6 +193,7 @@ function createWindow() {
newWindow.once("ready-to-show", () => {
newWindow.show();
});
postConfigure(newWindow);
},
},
{
Expand Down Expand Up @@ -220,6 +234,7 @@ function createWindow() {
newWindow.once("ready-to-show", () => {
newWindow.show();
});
postConfigure(newWindow);
}
}, 500);
},
Expand Down Expand Up @@ -267,6 +282,7 @@ function createWindow() {
newWindow.once("ready-to-show", () => {
newWindow.show();
});
postConfigure(newWindow);
},
},
{
Expand Down

0 comments on commit d19ffec

Please sign in to comment.