-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.js
416 lines (349 loc) · 9.75 KB
/
main.js
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// main.js
const { app, BrowserWindow, ipcMain, BrowserView, Menu, globalShortcut, Tray, shell, net } = require('electron');
const path = require('path');
const Store = require('electron-store');
const settings = new Store();
const isMac = process.platform === 'darwin';
let mainWindow;
let currentView;
let views = {};
let tray = null;
let settingsWindow = null;
let updateWindow = null;
const defaultShortcuts = isMac
? {
perplexityAI: 'Command+1',
perplexityLabs: 'Command+2',
minimizeApp: 'Command+M',
sendToTray: 'Command+T',
restoreApp: 'Command+Shift+T',
reload: 'Command+R',
}
: {
perplexityAI: 'Control+1',
perplexityLabs: 'Control+2',
minimizeApp: 'Control+M',
sendToTray: 'Control+T',
restoreApp: 'Control+Shift+T',
reload: 'Control+R',
};
let shortcuts = settings.get('shortcuts', defaultShortcuts);
let shortcutEnabled = settings.get('shortcutEnabled', true);
function registerShortcuts() {
if (!shortcutEnabled) return;
globalShortcut.unregisterAll();
const shortcutActions = {
perplexityAI: () => switchView('https://perplexity.ai'),
perplexityLabs: () => switchView('https://labs.perplexity.ai'),
minimizeApp: () => mainWindow.minimize(),
sendToTray: () => mainWindow.hide(),
restoreApp: () => {
if (mainWindow.isMinimized() || !mainWindow.isVisible()) {
mainWindow.show();
mainWindow.focus();
}
},
reload: () => {
if (currentView) {
currentView.webContents.reload();
}
},
};
for (const [key, shortcut] of Object.entries(shortcuts)) {
if (shortcutActions[key]) {
globalShortcut.register(shortcut, shortcutActions[key]);
}
}
}
function createWindow() {
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
preload: path.join(__dirname, 'src', 'js', 'preload', 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
},
});
Menu.setApplicationMenu(null);
mainWindow.loadFile('index.html').catch(console.error);
mainWindow.webContents.on('did-finish-load', () => {
mainWindow.show();
registerShortcuts();
loadDefaultAI();
checkForUpdates();
});
mainWindow.on('resize', adjustViewBounds);
mainWindow.on('close', (event) => {
const choice = require('electron').dialog.showMessageBoxSync(mainWindow, {
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you sure you want to quit?'
});
if (choice !== 0) {
event.preventDefault();
return;
}
});
}
function loadDefaultAI() {
const defaultAI = settings.get('defaultAI', 'https://perplexity.ai');
switchView(defaultAI);
}
function adjustViewBounds() {
if (currentView) {
const bounds = mainWindow.getContentBounds();
const sidebarWidth = 60;
currentView.setBounds({
x: sidebarWidth,
y: 0,
width: bounds.width - sidebarWidth,
height: bounds.height,
});
}
}
function switchView(url) {
if (url === 'refresh' && currentView) {
currentView.webContents.reload();
return;
}
if (currentView) {
mainWindow.removeBrowserView(currentView);
}
if (views[url]) {
currentView = views[url];
} else {
currentView = new BrowserView({
webPreferences: {
contextIsolation: true,
preload: path.join(__dirname, 'src', 'js', 'preload', 'preload_inject.js'),
},
});
currentView.webContents.loadURL(url);
views[url] = currentView;
currentView.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: 'deny' };
});
currentView.webContents.on('did-finish-load', () => {
currentView.webContents.executeJavaScript(`
(function removeNagScreens() {
const nagScreenSelectors = [
'div.max-w-\\\\[400px\\\\].rounded-xl',
'div.rounded-lg.p-md.animate-in.fade-in',
'div.flex.items-center.gap-sm',
];
nagScreenSelectors.forEach((selector) => {
document.querySelectorAll(selector).forEach((el) => el.remove());
});
})();
`);
});
currentView.webContents.on('did-start-loading', () => {
mainWindow.webContents.send('page-loading', true);
});
currentView.webContents.on('did-stop-loading', () => {
mainWindow.webContents.send('page-loading', false);
});
}
mainWindow.addBrowserView(currentView);
adjustViewBounds();
}
function createTray() {
let iconPath;
if (process.platform === 'win32') {
iconPath = path.join(__dirname, 'assets', 'icons', 'win', 'icon.ico');
} else if (process.platform === 'darwin') {
iconPath = path.join(__dirname, 'assets', 'icons', 'mac', 'favicon.icns');
} else {
iconPath = path.join(__dirname, 'assets', 'icons', 'png', 'favicon.png');
}
if (!fs.existsSync(iconPath)) {
console.error(`Tray icon not found at path: ${iconPath}`);
return;
}
try {
tray = new Tray(iconPath);
tray.setToolTip('Perplexity AI');
tray.on('click', () => {
if (mainWindow && (mainWindow.isMinimized() || !mainWindow.isVisible())) {
mainWindow.show();
}
mainWindow?.focus();
});
} catch (error) {
console.error('Failed to create tray:', error);
}
}
app.once('before-quit', () => {
app.quit();
});
module.exports = { createTray };
function checkForUpdates() {
const currentVersion = app.getVersion();
const request = net.request('https://raw.githubusercontent.com/inulute/perplexity-ai-app/main/package.json');
request.on('response', (response) => {
let body = '';
response.on('data', (chunk) => (body += chunk));
response.on('end', () => {
try {
const data = JSON.parse(body);
const latestVersion = data.version;
if (compareVersions(latestVersion, currentVersion) === 1) {
showUpdateWindow(latestVersion);
}
} catch (error) {
console.error('Error parsing latest version:', error);
}
});
});
request.on('error', console.error);
request.end();
}
function compareVersions(v1, v2) {
const parts1 = v1.split('.').map(Number);
const parts2 = v2.split('.').map(Number);
for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
const part1 = parts1[i] || 0;
const part2 = parts2[i] || 0;
if (part1 > part2) return 1;
if (part1 < part2) return -1;
}
return 0;
}
function showUpdateWindow(latestVersion) {
if (updateWindow) return;
updateWindow = new BrowserWindow({
width: 550,
height: 520,
parent: mainWindow,
modal: true,
show: false,
frame: false,
resizable: false,
webPreferences: {
preload: path.join(__dirname, 'src', 'js', 'preload', 'preload_update.js'),
contextIsolation: true,
nodeIntegration: false,
},
});
updateWindow.loadFile('update.html');
updateWindow.once('ready-to-show', () => {
updateWindow.webContents.send('latest-version', latestVersion);
updateWindow.show();
});
updateWindow.on('closed', () => {
updateWindow = null;
});
}
function detachAllShortcuts() {
globalShortcut.unregisterAll();
}
function reattachShortcuts() {
registerShortcuts();
}
ipcMain.on('get-shortcuts', (event) => {
event.sender.send('shortcuts', shortcuts);
});
ipcMain.on('set-shortcuts', (event, newShortcuts) => {
shortcuts = newShortcuts;
settings.set('shortcuts', shortcuts);
reattachShortcuts();
});
ipcMain.on('open-settings', () => {
detachAllShortcuts();
openSettingsWindow();
});
ipcMain.on('close-settings', () => {
if (settingsWindow) {
settingsWindow.close();
settingsWindow = null;
reattachShortcuts();
}
});
ipcMain.on('switch-ai-tool', (event, url) => {
if (url.startsWith('http') || url === 'refresh') {
switchView(url);
}
});
ipcMain.on('set-settings', (event, data) => {
if (data.shortcutEnabled !== undefined) {
shortcutEnabled = data.shortcutEnabled;
settings.set('shortcutEnabled', shortcutEnabled);
if (shortcutEnabled) {
registerShortcuts();
} else {
globalShortcut.unregisterAll();
}
}
});
ipcMain.on('get-settings', (event) => {
const data = {
shortcuts,
defaultAI: settings.get('defaultAI', 'https://perplexity.ai'),
shortcutEnabled,
};
event.sender.send('settings', data);
});
ipcMain.handle('get-app-version', () => app.getVersion());
ipcMain.on('open-external', (event, url) => {
shell.openExternal(url);
});
ipcMain.on('close-update-window', () => {
if (updateWindow) {
updateWindow.close();
updateWindow = null;
}
});
ipcMain.on('download-update', () => {
shell.openExternal('https://github.com/inulute/perplexity-ai-app/releases/latest');
});
function openSettingsWindow() {
if (settingsWindow) {
settingsWindow.focus();
return;
}
settingsWindow = new BrowserWindow({
width: 500,
height: 700,
parent: mainWindow,
modal: true,
frame: false,
webPreferences: {
preload: path.join(__dirname, 'src', 'js', 'preload', 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
},
});
settingsWindow.loadFile('settings.html').catch(console.error);
settingsWindow.on('closed', () => {
settingsWindow = null;
});
}
app.whenReady().then(() => {
if (!app.requestSingleInstanceLock()) {
app.quit();
} else {
createWindow();
createTray();
app.on('second-instance', () => {
if (mainWindow) {
if (!mainWindow.isVisible() || mainWindow.isMinimized()) {
mainWindow.show();
}
mainWindow.focus();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
} else {
mainWindow.show();
}
});
}
});
app.on('will-quit', () => {
globalShortcut.unregisterAll();
});