forked from vincelwt/chatgpt-mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
205 lines (182 loc) · 5.31 KB
/
index.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
const { menubar } = require("menubar");
const path = require("path");
const {
app,
nativeImage,
Tray,
Menu,
globalShortcut,
shell,
clipboard,
MenuItem,
} = require("electron");
const contextMenu = require("electron-context-menu");
const image = nativeImage.createFromPath(
path.join(__dirname, `images/newiconTemplate.png`)
);
app.on("ready", () => {
const tray = new Tray(image);
const mb = menubar({
browserWindow: {
icon: image,
transparent: path.join(__dirname, `images/iconApp.png`),
webPreferences: {
webviewTag: true,
// nativeWindowOpen: true,
},
minWidth: 400,
width: 500,
height: 650,
},
tray,
showOnAllWorkspaces: true,
preloadWindow: true,
showDockIcon: false,
icon: image,
});
mb.on("ready", () => {
const { window } = mb;
window.setAlwaysOnTop(true, "floating", 1);
if (process.platform !== "darwin") {
window.setSkipTaskbar(true);
} else {
app.dock.hide();
}
const contextMenuTemplate = [
// add links to github repo and vince's twitter
{
label: "Quit",
accelerator: "Command+Q",
click: () => {
app.quit();
},
},
{
label: "Reload",
accelerator: "Command+R",
click: () => {
window.reload();
},
},
{
label: "Open in browser",
click: () => {
shell.openExternal("https://chat.openai.com/");
},
},
{
type: "separator",
},
{
label: "View on GitHub",
click: () => {
shell.openExternal("https://github.com/one-data-cookie/chatgpt-mac");
},
},
];
tray.on("right-click", () => {
mb.tray.popUpContextMenu(Menu.buildFromTemplate(contextMenuTemplate));
});
tray.on("click", (e) => {
//check if ctrl or meta key is pressed while clicking
e.ctrlKey || e.metaKey
? mb.tray.popUpContextMenu(Menu.buildFromTemplate(contextMenuTemplate))
: null;
});
const menu = new Menu();
menu.append(new MenuItem({
label: 'Local Shortcuts',
submenu: [{
role: 'exit',
accelerator: 'Esc', click: () => { mb.hideWindow() }
}]
}))
globalShortcut.register("CommandOrControl+Alt+c", () => {
if (window.isVisible()) {
mb.hideWindow();
} else {
mb.showWindow();
if (process.platform == "darwin") {
mb.app.show();
}
mb.app.focus();
}
});
Menu.setApplicationMenu(menu);
// open devtools
// window.webContents.openDevTools();
console.log("Menubar app is ready.");
});
app.on("web-contents-created", (e, contents) => {
if (contents.getType() == "webview") {
// open link with external browser in webview
contents.on("new-window", (e, url) => {
e.preventDefault();
shell.openExternal(url);
});
// set context menu in webview
contextMenu({
window: contents,
});
// we can't set the native app menu with "menubar" so need to manually register these events
// register cmd+c/cmd+v events
contents.on("before-input-event", (event, input) => {
const { control, meta, key } = input;
// send message with enter
if (input.type !== "keyDown" && key == "Enter") {
// if shift is also pressed, ignore
if (input.shift) return;
// Imitate pressing Command+Enter
contents.sendInputEvent({ type: 'keyDown', keyCode: 'Enter', modifiers: ['command'] });
contents.sendInputEvent({ type: 'keyUp', keyCode: 'Enter', modifiers: ['command'] });
}
if (!control && !meta) return;
if (key === "x") contents.cut();
if (key === "c") contents.copy();
if (key === "v") contents.paste();
if (key === "a") contents.selectAll();
if (key === "z") contents.undo();
if (key === "y") contents.redo();
if (key === "q") app.quit();
if (key === "r") contents.reload();
if (key === "n") contents.loadURL('https://chat.openai.com/');
// work with current URL
const currentURL = contents.getURL();
if (key === "u") {
clipboard.writeText(currentURL);
setTimeout(() => {mb.window.hide();}, 100); // window hidden w 100ms delay
}
if (key === "o") {
shell.openExternal(currentURL);
setTimeout(() => {mb.window.hide();}, 100); // window hidden w 100ms delay
}
});
}
});
if (process.platform == "darwin") {
// restore focus to previous app on hiding
mb.on("after-hide", () => {
mb.app.hide();
});
}
// open links in new window
// app.on("web-contents-created", (event, contents) => {
// contents.on("will-navigate", (event, navigationUrl) => {
// event.preventDefault();
// shell.openExternal(navigationUrl);
// });
// });
// prevent background flickering
app.commandLine.appendSwitch(
"disable-backgrounding-occluded-windows",
"true"
);
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});