diff --git a/src/main.ts b/src/main.ts index 911db87..fdc1670 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,19 +9,17 @@ import { initDiscordRpc } from "./native/discordRpc"; import { initTray } from "./native/tray"; import { BUILD_URL, createMainWindow, mainWindow } from "./native/window"; -// Squirrel-specific logic -// create/remove shortcuts on Windows when installing / uninstalling -// we just need to close out of the app immediately +import * as fs from "fs"; +import * as path from "path"; + if (started) { app.quit(); } -// disable hw-accel if so requested if (!config.hardwareAcceleration) { app.disableHardwareAcceleration(); } -// ensure only one copy of the application can run const acquiredLock = app.requestSingleInstanceLock(); const onNotifyUser = (_info: IUpdateInfo) => { @@ -34,16 +32,24 @@ const onNotifyUser = (_info: IUpdateInfo) => { notification.show(); }; +const loadInject = () => { + if (!mainWindow) return; + mainWindow.webContents.on("dom-ready", async () => { + try { + const injectPath = path.join(__dirname, "inject.js"); + const code = fs.readFileSync(injectPath, "utf8"); + await mainWindow.webContents.executeJavaScript(code, true); + } catch {} + }); +}; + if (acquiredLock) { - // start auto update logic updateElectronApp({ onNotifyUser }); - // create and configure the app when electron is ready app.on("ready", () => { - // create window and application contexts createMainWindow(); + loadInject(); - // enable auto start on Windows and MacOS if (config.firstLaunch) { if (process.platform === "win32" || process.platform === "darwin") { autoLaunch.enable(); @@ -54,22 +60,17 @@ if (acquiredLock) { initTray(); initDiscordRpc(); - // Windows specific fix for notifications if (process.platform === "win32") { app.setAppUserModelId("chat.stoat.notifications"); } }); - // focus the window if we try to launch again app.on("second-instance", () => { mainWindow.show(); mainWindow.restore(); mainWindow.focus(); }); - // macOS specific behaviour to keep app active in dock: - // (irrespective of the minimise-to-tray option) - app.on("window-all-closed", () => { if (process.platform !== "darwin") { app.quit(); @@ -79,22 +80,20 @@ if (acquiredLock) { app.on("activate", () => { if (BrowserWindow.getAllWindows().length === 0) { createMainWindow(); + loadInject(); } else { mainWindow.show(); mainWindow.focus(); } }); - // ensure URLs launch in external context app.on("web-contents-created", (_, contents) => { - // prevent navigation out of build URL origin contents.on("will-navigate", (event, navigationUrl) => { if (new URL(navigationUrl).origin !== BUILD_URL.origin) { event.preventDefault(); } }); - // handle links externally contents.setWindowOpenHandler(({ url }) => { if ( url.startsWith("http:") ||