79 lines
2.4 KiB
JavaScript
79 lines
2.4 KiB
JavaScript
const { app, BrowserWindow, session } = require('electron');
|
|
const path = require('path');
|
|
const url = require('url');
|
|
|
|
function createWindow() {
|
|
// Create a persistent session for chattz to keep the user logged in
|
|
const sess = session.fromPartition('persist:chattz');
|
|
|
|
const win = new BrowserWindow({
|
|
width: 1200,
|
|
height: 800,
|
|
title: "Chattz Desktop",
|
|
webPreferences: {
|
|
nodeIntegration: false,
|
|
contextIsolation: true,
|
|
webSecurity: false, // Required for cross-origin fetch/ws from file:// with cookies
|
|
session: sess
|
|
}
|
|
});
|
|
|
|
const backendUrl = (process.env.CHATTZ_URL || 'https://discord.flegr.me').replace(/\/$/, '');
|
|
const indexPath = path.join(__dirname, 'desktop', 'index.html');
|
|
|
|
// Construct the local file URL with the backend parameter
|
|
const localUrl = url.format({
|
|
pathname: indexPath,
|
|
protocol: 'file:',
|
|
slashes: true,
|
|
query: { backend: backendUrl }
|
|
});
|
|
|
|
console.log(`Loading desktop app from: ${localUrl}`);
|
|
|
|
const loadDesktopApp = () => {
|
|
win.loadURL(localUrl).catch((err) => {
|
|
console.error(`Failed to load desktop file: ${err}`);
|
|
});
|
|
};
|
|
|
|
// Use a navigation listener to detect when the remote login is complete
|
|
win.webContents.on('will-navigate', (event, navigatedUrl) => {
|
|
const normalizedNav = navigatedUrl.replace(/\/$/, '');
|
|
// If the user lands back on the remote root, they are logged in.
|
|
// Redirect them back to our local bugfixed desktop UI.
|
|
if (normalizedNav === backendUrl) {
|
|
console.log("Detected remote landing (login success), returning to desktop UI...");
|
|
event.preventDefault();
|
|
loadDesktopApp();
|
|
}
|
|
});
|
|
|
|
win.webContents.on('did-navigate', (event, navigatedUrl) => {
|
|
const normalizedNav = navigatedUrl.replace(/\/$/, '');
|
|
if (normalizedNav === backendUrl) {
|
|
loadDesktopApp();
|
|
}
|
|
});
|
|
|
|
loadDesktopApp();
|
|
|
|
// Uncomment to debug
|
|
// win.webContents.openDevTools();
|
|
}
|
|
|
|
app.whenReady().then(() => {
|
|
createWindow();
|
|
|
|
app.on('activate', () => {
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
createWindow();
|
|
}
|
|
});
|
|
});
|
|
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit();
|
|
}
|
|
});
|