Inject Avia client via inject.js
Signed-off-by: AvaLilac <amyshimplays@gmail.com>
This commit is contained in:
parent
b57faa2c59
commit
fa3bdc7019
1 changed files with 16 additions and 17 deletions
33
src/main.ts
33
src/main.ts
|
|
@ -9,19 +9,17 @@ import { initDiscordRpc } from "./native/discordRpc";
|
||||||
import { initTray } from "./native/tray";
|
import { initTray } from "./native/tray";
|
||||||
import { BUILD_URL, createMainWindow, mainWindow } from "./native/window";
|
import { BUILD_URL, createMainWindow, mainWindow } from "./native/window";
|
||||||
|
|
||||||
// Squirrel-specific logic
|
import * as fs from "fs";
|
||||||
// create/remove shortcuts on Windows when installing / uninstalling
|
import * as path from "path";
|
||||||
// we just need to close out of the app immediately
|
|
||||||
if (started) {
|
if (started) {
|
||||||
app.quit();
|
app.quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
// disable hw-accel if so requested
|
|
||||||
if (!config.hardwareAcceleration) {
|
if (!config.hardwareAcceleration) {
|
||||||
app.disableHardwareAcceleration();
|
app.disableHardwareAcceleration();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure only one copy of the application can run
|
|
||||||
const acquiredLock = app.requestSingleInstanceLock();
|
const acquiredLock = app.requestSingleInstanceLock();
|
||||||
|
|
||||||
const onNotifyUser = (_info: IUpdateInfo) => {
|
const onNotifyUser = (_info: IUpdateInfo) => {
|
||||||
|
|
@ -34,16 +32,24 @@ const onNotifyUser = (_info: IUpdateInfo) => {
|
||||||
notification.show();
|
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) {
|
if (acquiredLock) {
|
||||||
// start auto update logic
|
|
||||||
updateElectronApp({ onNotifyUser });
|
updateElectronApp({ onNotifyUser });
|
||||||
|
|
||||||
// create and configure the app when electron is ready
|
|
||||||
app.on("ready", () => {
|
app.on("ready", () => {
|
||||||
// create window and application contexts
|
|
||||||
createMainWindow();
|
createMainWindow();
|
||||||
|
loadInject();
|
||||||
|
|
||||||
// enable auto start on Windows and MacOS
|
|
||||||
if (config.firstLaunch) {
|
if (config.firstLaunch) {
|
||||||
if (process.platform === "win32" || process.platform === "darwin") {
|
if (process.platform === "win32" || process.platform === "darwin") {
|
||||||
autoLaunch.enable();
|
autoLaunch.enable();
|
||||||
|
|
@ -54,22 +60,17 @@ if (acquiredLock) {
|
||||||
initTray();
|
initTray();
|
||||||
initDiscordRpc();
|
initDiscordRpc();
|
||||||
|
|
||||||
// Windows specific fix for notifications
|
|
||||||
if (process.platform === "win32") {
|
if (process.platform === "win32") {
|
||||||
app.setAppUserModelId("chat.stoat.notifications");
|
app.setAppUserModelId("chat.stoat.notifications");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// focus the window if we try to launch again
|
|
||||||
app.on("second-instance", () => {
|
app.on("second-instance", () => {
|
||||||
mainWindow.show();
|
mainWindow.show();
|
||||||
mainWindow.restore();
|
mainWindow.restore();
|
||||||
mainWindow.focus();
|
mainWindow.focus();
|
||||||
});
|
});
|
||||||
|
|
||||||
// macOS specific behaviour to keep app active in dock:
|
|
||||||
// (irrespective of the minimise-to-tray option)
|
|
||||||
|
|
||||||
app.on("window-all-closed", () => {
|
app.on("window-all-closed", () => {
|
||||||
if (process.platform !== "darwin") {
|
if (process.platform !== "darwin") {
|
||||||
app.quit();
|
app.quit();
|
||||||
|
|
@ -79,22 +80,20 @@ if (acquiredLock) {
|
||||||
app.on("activate", () => {
|
app.on("activate", () => {
|
||||||
if (BrowserWindow.getAllWindows().length === 0) {
|
if (BrowserWindow.getAllWindows().length === 0) {
|
||||||
createMainWindow();
|
createMainWindow();
|
||||||
|
loadInject();
|
||||||
} else {
|
} else {
|
||||||
mainWindow.show();
|
mainWindow.show();
|
||||||
mainWindow.focus();
|
mainWindow.focus();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// ensure URLs launch in external context
|
|
||||||
app.on("web-contents-created", (_, contents) => {
|
app.on("web-contents-created", (_, contents) => {
|
||||||
// prevent navigation out of build URL origin
|
|
||||||
contents.on("will-navigate", (event, navigationUrl) => {
|
contents.on("will-navigate", (event, navigationUrl) => {
|
||||||
if (new URL(navigationUrl).origin !== BUILD_URL.origin) {
|
if (new URL(navigationUrl).origin !== BUILD_URL.origin) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// handle links externally
|
|
||||||
contents.setWindowOpenHandler(({ url }) => {
|
contents.setWindowOpenHandler(({ url }) => {
|
||||||
if (
|
if (
|
||||||
url.startsWith("http:") ||
|
url.startsWith("http:") ||
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue