Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
514 lines (450 loc) · 14.6 KB
/
Copy pathmain.js
File metadata and controls
514 lines (450 loc) · 14.6 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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
const { app, BrowserWindow, session, shell, ipcMain, dialog, Menu } = require("electron");
const net = require("net");
const path = require("path");
const { autoUpdater } = require("electron-updater");
const { loadCredentials, saveCredentials, clearCredentials } = require("./src/credentials");
const { tryExtractChromeCookies } = require("./src/browser-cookies");
const isDev = !app.isPackaged;
const DATA_DIR = isDev ? __dirname : app.getPath("userData");
let mainWindow = null;
let loginWindow = null;
let serverPort = null;
// --- Port discovery ---
function findFreePort() {
return new Promise((resolve) => {
const srv = net.createServer();
srv.listen(0, "127.0.0.1", () => {
const { port } = srv.address();
srv.close(() => resolve(port));
});
});
}
// --- Window management ---
function openMainWindow(port) {
const isWin = process.platform === "win32";
mainWindow = new BrowserWindow({
width: 1440,
height: 900,
minWidth: 800,
minHeight: 600,
titleBarStyle: isWin ? "default" : "hiddenInset",
icon: path.join(__dirname, "assets/AppIcon.png"),
webPreferences: {
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
nodeIntegration: false,
autoplayPolicy: "no-user-gesture-required",
},
});
// External links → system browser
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: "deny" };
});
mainWindow.webContents.on("will-navigate", (event, url) => {
if (!url.startsWith(`http://localhost:${port}`)) {
event.preventDefault();
shell.openExternal(url);
}
});
mainWindow.loadURL(`http://localhost:${port}`);
mainWindow.on("closed", () => { mainWindow = null; });
}
function openLoginWindow(port, autoLogin = false) {
loginWindow = new BrowserWindow({
width: 1000,
height: 720,
title: "Connect Your X Account",
icon: path.join(__dirname, "assets/AppIcon.png"),
webPreferences: {
preload: path.join(__dirname, "preload.js"),
contextIsolation: true,
nodeIntegration: false,
},
});
let credentialsCaptured = false;
// Intercept Twitter GraphQL requests to capture auth credentials
loginWindow.webContents.session.webRequest.onBeforeSendHeaders(
{ urls: ["*://x.com/i/api/graphql/*", "*://twitter.com/i/api/graphql/*"] },
(details, callback) => {
// Always pass through — never block the request
callback({ requestHeaders: details.requestHeaders });
const opMatch = details.url.match(/graphql\/([^/?]+)\/([^/?]+)/);
if (!opMatch) return;
const queryId = opMatch[1];
const operation = opMatch[2];
if (operation === "DeleteBookmark") {
const existing = loadCredentials(DATA_DIR) || {};
saveCredentials(DATA_DIR, {
...existing,
deleteBookmarkQueryId: queryId,
queryIds: { ...(existing.queryIds || {}), DeleteBookmark: queryId },
});
return;
}
if (credentialsCaptured) return;
// Only the Bookmarks operation carries everything needed to bootstrap
// the app session.
if (operation !== "Bookmarks") return;
const h = details.requestHeaders;
const bearerRaw = h["authorization"] || h["Authorization"] || "";
const bearerToken = bearerRaw.replace(/^Bearer\s+/i, "").trim();
const ct0 = (h["x-csrf-token"] || h["X-Csrf-Token"] || "").trim();
const cookies = h["cookie"] || h["Cookie"] || "";
const authTokenMatch = cookies.match(/auth_token=([^;]+)/);
const authToken = authTokenMatch ? authTokenMatch[1].trim() : "";
if (!bearerToken || !ct0 || !authToken || !queryId) return;
credentialsCaptured = true;
const creds = {
bearerToken,
ct0,
authToken,
queryId,
capturedAt: new Date().toISOString(),
};
saveCredentials(DATA_DIR, creds);
console.log("✓ Credentials captured, queryId:", queryId);
// Fetch user info in background, then open main window
const { fetchUserInfo } = require("./src/twitter");
fetchUserInfo(creds).then((userInfo) => {
if (userInfo.userHandle) {
saveCredentials(DATA_DIR, { ...creds, ...userInfo });
console.log("✓ Logged in as @" + userInfo.userHandle);
}
if (loginWindow) { loginWindow.close(); loginWindow = null; }
openMainWindow(port);
}).catch(() => {
if (loginWindow) { loginWindow.close(); loginWindow = null; }
openMainWindow(port);
});
}
);
// Auto-redirect to bookmarks once user is logged in (auth_token cookie appears)
loginWindow.webContents.session.cookies.on("changed", (event, cookie) => {
if (
(cookie.domain.includes("twitter.com") || cookie.domain.includes("x.com")) &&
cookie.name === "auth_token" &&
!cookie.removed &&
!credentialsCaptured
) {
setTimeout(() => {
if (loginWindow && !credentialsCaptured) {
loginWindow.loadURL("https://twitter.com/i/bookmarks");
}
}, 1500);
}
});
loginWindow.on("closed", () => {
loginWindow = null;
// If closed before capture and no main window, quit
if (!mainWindow && !credentialsCaptured) {
dialog.showErrorBox(
"Connection cancelled",
"You closed the login window before connecting. Reopen the app to try again."
);
app.quit();
}
});
if (autoLogin) {
// Cookies were pre-injected into the session; going straight to bookmarks
// triggers the GraphQL request that the interceptor above captures.
// If cookies are stale, X will redirect to the login page in this same
// window — user can fall back to manual login without us doing anything.
loginWindow.loadURL("https://x.com/i/bookmarks");
} else {
loginWindow.loadFile(path.join(__dirname, "login.html"));
}
}
async function injectChromeCookies({ ct0, authToken }) {
const ses = session.defaultSession;
await ses.cookies.set({
url: "https://x.com/",
name: "ct0",
value: ct0,
domain: ".x.com",
path: "/",
secure: true,
sameSite: "lax",
});
await ses.cookies.set({
url: "https://x.com/",
name: "auth_token",
value: authToken,
domain: ".x.com",
path: "/",
secure: true,
httpOnly: true,
sameSite: "lax",
});
}
// Show a native dialog offering to reuse the user's Chrome session.
// Returns true if the user accepted, false if they want manual login.
function promptUseChromeSession() {
const choice = dialog.showMessageBoxSync(null, {
type: "question",
title: "Chrome session detected",
message: "We found an active X session in Chrome",
detail:
"Would you like to sign in automatically using your Chrome session? " +
"Otherwise you can sign in manually in the next window.",
buttons: ["Use Chrome session", "Sign in manually"],
defaultId: 0,
cancelId: 1,
noLink: true,
});
return choice === 0;
}
// Start the correct window flow when no credentials are stored:
// offer the Chrome session shortcut if one exists, otherwise open the
// manual login window.
async function beginAuthFlow() {
const chromeCookies = tryExtractChromeCookies();
if (chromeCookies && promptUseChromeSession()) {
await injectChromeCookies(chromeCookies);
openLoginWindow(serverPort, /*autoLogin=*/ true);
} else {
openLoginWindow(serverPort);
}
}
// --- IPC handlers ---
ipcMain.handle("re-login", async () => {
clearCredentials(DATA_DIR);
if (mainWindow) { mainWindow.close(); mainWindow = null; }
await beginAuthFlow();
});
ipcMain.handle("app-version", () => app.getVersion());
ipcMain.handle("check-for-updates", () => {
checkForUpdatesManually();
});
// Open URL in the user's default browser. Routed through IPC because
// `shell` is not available in sandboxed preload (Electron 20+ default).
ipcMain.handle("open-external", (_event, url) => {
if (typeof url !== "string") return;
if (!/^https?:\/\//i.test(url)) return;
return shell.openExternal(url);
});
// Pop up the native macOS share sheet (AirDrop, Messages, Mail, Notes, etc.)
// anchored to the mouse position. macOS-only; returns false on other platforms
// so the renderer can fall back to clipboard copy.
ipcMain.handle("share-url", (event, { url, title, text }) => {
if (process.platform !== "darwin") return false;
const win = BrowserWindow.fromWebContents(event.sender);
if (!win) return false;
const sharingItem = { urls: [url] };
// Bundle title + text as a single text payload so Messages/Notes include it
const combined = [title, text].filter(Boolean).join("\n").trim();
if (combined) sharingItem.texts = [combined];
const menu = Menu.buildFromTemplate([
{ label: "Share…", role: "shareMenu", sharingItem },
]);
menu.popup({ window: win });
return true;
});
// --- Auto-update ---
function setupAutoUpdater() {
if (isDev) {
console.log("[updater] Skipped in development mode");
return;
}
autoUpdater.autoDownload = true;
autoUpdater.autoInstallOnAppQuit = true;
autoUpdater.on("error", (err) => {
console.error("[updater] error:", err?.message || err);
});
autoUpdater.on("checking-for-update", () => {
console.log("[updater] Checking for updates...");
});
autoUpdater.on("update-available", (info) => {
console.log("[updater] update available:", info.version);
dialog.showMessageBox({
type: "info",
title: "Update Available",
message: `A new version ${info.version} is available!`,
detail: "The update will be downloaded in the background. You'll be notified when it's ready to install.",
buttons: ["OK"],
});
});
autoUpdater.on("update-not-available", (info) => {
console.log("[updater] up to date:", info.version);
});
autoUpdater.on("download-progress", (progress) => {
console.log(`[updater] Download progress: ${Math.round(progress.percent)}%`);
});
autoUpdater.on("update-downloaded", (info) => {
console.log("[updater] downloaded:", info.version);
const response = dialog.showMessageBoxSync({
type: "info",
title: "Update Ready",
message: `Version ${info.version} has been downloaded.`,
detail: "The update will be installed when you quit the app. Would you like to restart now?",
buttons: ["Restart Now", "Later"],
defaultId: 0,
cancelId: 1,
});
if (response === 0) {
autoUpdater.quitAndInstall(false, true);
}
});
// Check for updates on startup
setTimeout(() => {
autoUpdater.checkForUpdates().catch((err) => {
console.error("[updater] Failed to check for updates:", err);
});
}, 3000);
}
// Show custom About dialog
function showAboutDialog() {
const version = app.getVersion();
dialog.showMessageBox({
type: "info",
title: `About ${app.name}`,
message: `${app.name}`,
detail: `Version ${version}\n\nBrowse your X bookmarks in a beautiful visual desktop library.\n\n© 2026 Hasan Aydın`,
buttons: ["OK"],
icon: path.join(__dirname, "assets/AppIcon.png"),
});
}
// Manual update check
function checkForUpdatesManually() {
if (isDev) {
dialog.showMessageBox({
type: "info",
title: "Development Mode",
message: "Auto-update is disabled in development mode.",
buttons: ["OK"],
});
return;
}
autoUpdater.checkForUpdates().then((result) => {
if (!result || !result.updateInfo) {
dialog.showMessageBox({
type: "info",
title: "No Updates",
message: "You're already running the latest version!",
buttons: ["OK"],
});
}
}).catch((err) => {
dialog.showErrorBox("Update Check Failed", `Could not check for updates: ${err?.message || err}`);
});
}
// --- Menu setup ---
function createAppMenu() {
const isMac = process.platform === "darwin";
const template = [
...(isMac ? [{
label: app.name,
submenu: [
{
label: `About ${app.name}`,
click: () => showAboutDialog(),
},
{ type: "separator" },
{
label: "Check for Updates...",
click: () => checkForUpdatesManually(),
},
{ type: "separator" },
{ role: "services" },
{ type: "separator" },
{ role: "hide" },
{ role: "hideOthers" },
{ role: "unhide" },
{ type: "separator" },
{ role: "quit" },
],
}] : []),
{
label: "Edit",
submenu: [
{ role: "undo" },
{ role: "redo" },
{ type: "separator" },
{ role: "cut" },
{ role: "copy" },
{ role: "paste" },
...(isMac ? [
{ role: "pasteAndMatchStyle" },
{ role: "delete" },
{ role: "selectAll" },
] : [
{ role: "delete" },
{ type: "separator" },
{ role: "selectAll" },
]),
],
},
{
label: "View",
submenu: [
{ role: "reload" },
{ role: "forceReload" },
{ role: "toggleDevTools" },
{ type: "separator" },
{ role: "resetZoom" },
{ role: "zoomIn" },
{ role: "zoomOut" },
{ type: "separator" },
{ role: "togglefullscreen" },
],
},
{
label: "Window",
submenu: [
{ role: "minimize" },
{ role: "zoom" },
...(isMac ? [
{ type: "separator" },
{ role: "front" },
{ type: "separator" },
{ role: "window" },
] : [
{ role: "close" },
]),
],
},
...(!isMac ? [{
label: "Help",
submenu: [
{
label: `About ${app.name}`,
click: () => showAboutDialog(),
},
{ type: "separator" },
{
label: "Check for Updates...",
click: () => checkForUpdatesManually(),
},
],
}] : []),
];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
// --- App startup ---
app.whenReady().then(async () => {
serverPort = await findFreePort();
// Start HTTP server (OG proxy, video proxy, static files, /sync SSE)
const { createServer } = require("./server");
createServer(serverPort, DATA_DIR);
// Setup application menu
createAppMenu();
const creds = loadCredentials(DATA_DIR);
if (creds) {
openMainWindow(serverPort);
} else {
await beginAuthFlow();
}
setupAutoUpdater();
});
app.on("window-all-closed", () => {
// On macOS, keep app running unless explicitly quit
if (process.platform !== "darwin") app.quit();
});
app.on("activate", async () => {
if (!mainWindow && !loginWindow && serverPort) {
const creds = loadCredentials(DATA_DIR);
if (creds) openMainWindow(serverPort);
else await beginAuthFlow();
}
});