-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.js
129 lines (106 loc) · 3.29 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
const { app, BrowserWindow, shell, globalShortcut } = require('electron');
const path = require('path');
app.allowRendererProcessReuse = true;
let windows = {};
let mainWindow;
const createWindow = (name, url) => {
const win = new BrowserWindow({
width: 1000,
height: 758,
backgroundColor: "#272829",
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
},
autoHideMenuBar: true,
});
win.loadURL(url);
// Handle external links
win.webContents.setWindowOpenHandler(({ url }) => {
const isExternal = /^https?:\/\//i.test(url);
if (isExternal) {
shell.openExternal(url).catch(err => console.error('Failed to open URL:', err));
return { action: 'deny' }; // Prevent Electron from creating a new window
}
return { action: 'allow' }; // Allow internal links to open in the window
});
win.on('closed', () => {
windows[name] = null;
});
return win;
};
const minimizeAllWindows = () => {
for (const key in windows) {
if (windows[key]) {
windows[key].minimize();
}
}
};
const restoreAllWindows = () => {
for (const key in windows) {
if (windows[key] && windows[key].isMinimized()) {
windows[key].restore();
}
}
};
const areAllWindowsMinimized = () => {
return Object.values(windows).every(win => win && win.isMinimized());
};
app.on('ready', () => {
// Create the main window
mainWindow = createWindow('main', 'file://' + path.join(__dirname, 'initial.html'));
windows['main'] = mainWindow;
// Open all external links in default browser
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
const isExternal = /^https?:\/\//i.test(url);
if (isExternal) {
shell.openExternal(url).catch(err => console.error('Failed to open URL:', err));
return { action: 'deny' }; // Prevent Electron from creating a new window
}
return { action: 'allow' }; // Allow internal links to open in the window
});
// Handle URL redirections
mainWindow.webContents.on('did-navigate', (event, url) => {
if (url.includes('https://www.perplexity.ai/auth/verify-request')) {
mainWindow.loadFile(path.join(__dirname, 'url-open.html'));
}
});
mainWindow.webContents.on('did-navigate-in-page', (event, url) => {
if (url.includes('https://www.perplexity.ai/auth/verify-request')) {
mainWindow.loadFile(path.join(__dirname, 'url-open.html'));
}
});
// Minimize or restore all windows
globalShortcut.register('CmdOrCtrl+M', () => {
if (areAllWindowsMinimized()) {
restoreAllWindows();
} else {
minimizeAllWindows();
}
});
// Open or restore Perplexity Labs
globalShortcut.register('CmdOrCtrl+7', () => {
if (windows['labs']) {
windows['labs'].restore();
windows['labs'].focus();
} else {
windows['labs'] = createWindow('labs', 'https://labs.perplexity.ai');
}
});
// Open or restore Perplexity AI
globalShortcut.register('CmdOrCtrl+6', () => {
if (windows['ai']) {
windows['ai'].restore();
windows['ai'].focus();
} else {
windows['ai'] = createWindow('ai', 'https://perplexity.ai');
}
});
mainWindow.on('closed', () => {
windows['main'] = null;
});
});
app.on('window-all-closed', () => {
app.quit();
});