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 { ICON_PATH } from "../shared/paths";
export function createMainWindow() {
let isQuitting = false;
let isQuitting = false;
const win = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: false,
sandbox: false,
contextIsolation: true,
devTools: true,
preload: join(__dirname, "preload.js")
},
icon: ICON_PATH
app.on("before-quit", () => {
isQuitting = true;
});
function initWindowOpenHandler(win: BrowserWindow) {
win.webContents.setWindowOpenHandler(({ url }) => {
switch (url) {
case "about:blank":
case "https://discord.com/popout":
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([
{
label: "Open",
@ -39,8 +58,27 @@ export function createMainWindow() {
tray.setContextMenu(trayMenu);
tray.on("click", () => win.show());
app.on("before-quit", () => {
isQuitting = true;
win.on("show", () => {
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 => {
@ -52,14 +90,8 @@ export function createMainWindow() {
return false;
});
win.on("show", () => {
trayMenu.items[0].enabled = false;
});
win.on("hide", () => {
trayMenu.items[0].enabled = true;
});
initTray(win);
initWindowOpenHandler(win);
win.loadURL("https://discord.com/app");