forked from Tomobobo710/SimpleChatJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelectron-main.js
More file actions
241 lines (212 loc) · 7.58 KB
/
Copy pathelectron-main.js
File metadata and controls
241 lines (212 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
const { app, BrowserWindow, Menu, shell, ipcMain } = require('electron');
const path = require('path');
// Set Chromium user-data-dir BEFORE app initialization
const isPackaged = app.isPackaged;
const appPath = isPackaged ? path.dirname(process.execPath) : __dirname;
const userdataPath = path.join(appPath, 'userdata');
const electronDataPath = path.join(userdataPath, 'electron');
// Create directories BEFORE setting user-data-dir
const fs = require('fs');
if (!fs.existsSync(userdataPath)) {
fs.mkdirSync(userdataPath, { recursive: true });
}
if (!fs.existsSync(electronDataPath)) {
fs.mkdirSync(electronDataPath, { recursive: true });
}
// Force Chromium to use our portable path and disable encryption features
console.log('Setting Chromium user-data-dir to:', electronDataPath);
app.commandLine.appendSwitch('user-data-dir', electronDataPath);
console.log('All command line switches:', process.argv);
app.commandLine.appendSwitch('disable-features', 'VizDisplayCompositor');
app.commandLine.appendSwitch('use-fake-device-for-media-stream');
app.commandLine.appendSwitch('disable-dev-shm-usage');
app.commandLine.appendSwitch('no-first-run');
app.commandLine.appendSwitch('disable-default-apps');
app.commandLine.appendSwitch('disable-extensions');
app.commandLine.appendSwitch('disable-component-extensions-with-background-pages');
// Keep a global reference of the window object
let mainWindow;
// Setup portable paths AFTER early Chromium config
function setupPortablePaths() {
console.log('Setting up portable paths...');
console.log('App path:', appPath);
console.log('Userdata path:', userdataPath);
console.log('Electron data path:', electronDataPath);
// Force Electron to use a subfolder for its own data
app.setPath('userData', electronDataPath);
// Set environment variable for the backend (main userdata folder)
process.env.PORTABLE_USERDATA_PATH = userdataPath;
// Create userdata directories if they don't exist
const fs = require('fs');
if (!fs.existsSync(userdataPath)) {
fs.mkdirSync(userdataPath, { recursive: true });
console.log('Created userdata directory');
}
if (!fs.existsSync(electronDataPath)) {
fs.mkdirSync(electronDataPath, { recursive: true });
console.log('Created electron data directory');
}
}
// Start the Express server directly (no spawn)
function startServer() {
console.log('Starting SimpleChatJS server directly...');
try {
// Import and start the server directly in this process
require('./backend/server.js');
console.log('Server started successfully');
} catch (error) {
console.error('Server startup error:', error);
}
}
// Create the main application window
function createWindow() {
// Create the browser window
mainWindow = new BrowserWindow({
width: 1400,
height: 900,
minWidth: 800,
minHeight: 600,
icon: path.join(__dirname, 'simplechatjs.ico'),
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
enableRemoteModule: false,
spellcheck: true,
preload: path.join(__dirname, 'electron-preload.js')
},
show: false // Don't show until ready
});
// Wait a moment for server to start, then load the URL
setTimeout(() => {
mainWindow.loadURL('http://localhost:50505');
mainWindow.show();
}, 2000);
// Open DevTools in development
if (process.argv.includes('--dev')) {
mainWindow.webContents.openDevTools();
}
// Handle window closed
mainWindow.on('closed', () => {
mainWindow = null;
});
// Handle navigation - keep user in the app
mainWindow.webContents.on('new-window', (event, navigationUrl) => {
event.preventDefault();
require('electron').shell.openExternal(navigationUrl);
});
// Enable context menu (right-click menu)
mainWindow.webContents.on('context-menu', (event, params) => {
// Build menu items array
const menuItems = [];
// Add copy menu item if there is text selection
if (params.selectionText) {
menuItems.push({ action: 'copy', label: 'Copy' });
}
// Add paste menu item if we're in an editable field
if (params.isEditable) {
if (params.selectionText) {
menuItems.push({ action: 'cut', label: 'Cut' });
}
menuItems.push({ action: 'paste', label: 'Paste' });
}
// Add inspect element in development mode
if (process.argv.includes('--dev')) {
if (menuItems.length > 0) {
menuItems.push({ action: 'separator' });
}
menuItems.push({ action: 'inspect', label: 'Inspect Element' });
}
// Send context menu data to renderer if we have items
if (menuItems.length > 0) {
mainWindow.webContents.send('show-context-menu', {
x: params.x,
y: params.y,
items: menuItems
});
}
});
}
// Remove default menu bar
function createMenu() {
const template = [
{
label: 'SimpleChatJS',
submenu: [
{
label: 'About SimpleChatJS',
role: 'about'
},
{ type: 'separator' },
{
label: 'Quit',
accelerator: 'CmdOrCtrl+Q',
click: () => {
app.quit();
}
}
]
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' }
]
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
{ role: 'resetZoom' },
{ role: 'zoomIn' },
{ role: 'zoomOut' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
}
];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
// App event handlers
app.whenReady().then(() => {
setupPortablePaths(); // Setup paths FIRST
startServer(); // Start server directly
createWindow(); // Create window
Menu.setApplicationMenu(null); // Remove menu bar completely
// Handle inspect element IPC
ipcMain.on('inspect-element', () => {
if (mainWindow) {
mainWindow.webContents.toggleDevTools();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
// Server runs in the same process, so it'll quit with the app
// On macOS, keep app running even when all windows are closed
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('before-quit', () => {
// Server runs in the same process, so no cleanup needed
console.log('App shutting down...');
});
// Security: Prevent new window creation
app.on('web-contents-created', (event, contents) => {
contents.on('new-window', (event, navigationUrl) => {
event.preventDefault();
});
});