Make URLs open in browser instead of a new Electron window

This commit is contained in:
Vendicated 2023-04-04 01:47:33 +02:00
parent 1c20f35460
commit d7020a8501
No known key found for this signature in database
GPG key ID: A1DC0CFB5615D905

View file

@ -1,22 +1,41 @@
import { BrowserWindow, Menu, Tray, app } from "electron"; import { BrowserWindow, Menu, Tray, app, shell } from "electron";
import { join } from "path"; import { join } from "path";
import { ICON_PATH } from "../shared/paths"; import { ICON_PATH } from "../shared/paths";
export function createMainWindow() { let isQuitting = false;
let isQuitting = false;
const win = new BrowserWindow({ app.on("before-quit", () => {
show: false, isQuitting = true;
webPreferences: { });
nodeIntegration: false,
sandbox: false, function initWindowOpenHandler(win: BrowserWindow) {
contextIsolation: true, win.webContents.setWindowOpenHandler(({ url }) => {
devTools: true, switch (url) {
preload: join(__dirname, "preload.js") case "about:blank":
}, case "https://discord.com/popout":
icon: ICON_PATH return { action: "allow" };
}
try {
var protocol = new URL(url).protocol;
} catch {
return { action: "deny" };
}
switch (protocol) {
case "http:":
case "https:":
case "mailto:":
case "steam:":
case "spotify:":
shell.openExternal(url);
}
return { action: "deny" };
}); });
}
function initTray(win: BrowserWindow) {
const trayMenu = Menu.buildFromTemplate([ const trayMenu = Menu.buildFromTemplate([
{ {
label: "Open", label: "Open",
@ -39,8 +58,27 @@ export function createMainWindow() {
tray.setContextMenu(trayMenu); tray.setContextMenu(trayMenu);
tray.on("click", () => win.show()); tray.on("click", () => win.show());
app.on("before-quit", () => { win.on("show", () => {
isQuitting = true; trayMenu.items[0].enabled = false;
});
win.on("hide", () => {
trayMenu.items[0].enabled = true;
});
}
export function createMainWindow() {
const win = new BrowserWindow({
show: false,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: false,
sandbox: false,
contextIsolation: true,
devTools: true,
preload: join(__dirname, "preload.js")
},
icon: ICON_PATH
}); });
win.on("close", e => { win.on("close", e => {
@ -52,14 +90,8 @@ export function createMainWindow() {
return false; return false;
}); });
win.on("show", () => { initTray(win);
trayMenu.items[0].enabled = false; initWindowOpenHandler(win);
});
win.on("hide", () => {
trayMenu.items[0].enabled = true;
});
win.loadURL("https://discord.com/app"); win.loadURL("https://discord.com/app");