Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
140 lines (123 loc) · 3.62 KB
/
Copy pathmain.js
File metadata and controls
140 lines (123 loc) · 3.62 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
const { app, BrowserWindow, shell, Menu, session } = require("electron");
const path = require("path");
// Sandbox is enabled by default for security
// Disable menu bar for minimal look
Menu.setApplicationMenu(null);
let mainWindow;
function createWindow() {
const iconPath = path.join(__dirname, "assets/whatsapp.svg");
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 400,
minHeight: 300,
title: "Linked - WhatsApp",
icon: iconPath,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
webviewTag: false,
spellcheck: true,
partition: "persist:whatsapp",
sandbox: true,
preload: undefined,
},
// Clean minimal look - no frame decorations like VS Code
frame: false,
autoHideMenuBar: true,
backgroundColor: "#000000",
show: false,
titleBarStyle: "hidden",
titleBarOverlay: false,
});
// Use Electron's Chrome version for user agent to ensure compatibility
const electronVersion = process.versions.chrome;
const userAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${electronVersion} Safari/537.36`;
mainWindow.webContents.setUserAgent(userAgent);
// Load WhatsApp Web directly
mainWindow.loadURL("https://web.whatsapp.com");
// Security: Prevent data exfiltration via webRequest
mainWindow.webContents.session.webRequest.onBeforeSendHeaders(
(details, callback) => {
const url = new URL(details.url);
// Only allow requests to WhatsApp domains
const allowedDomains = [
"web.whatsapp.com",
"whatsapp.com",
"whatsapp.net",
];
const isAllowed = allowedDomains.some((domain) =>
url.hostname.endsWith(domain),
);
if (!isAllowed) {
console.warn(`Blocked external request to: ${details.url}`);
}
callback({ requestHeaders: details.requestHeaders });
},
);
// Show window when ready to prevent visual flash
mainWindow.once("ready-to-show", () => {
mainWindow.show();
});
// Handle external links - open in default browser
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: "deny" };
});
// Handle navigation to external URLs
mainWindow.webContents.on("will-navigate", (event, url) => {
try {
const parsedUrl = new URL(url);
const isWhatsApp =
parsedUrl.hostname === "web.whatsapp.com" &&
parsedUrl.protocol === "https:";
if (!isWhatsApp) {
event.preventDefault();
shell.openExternal(url);
}
} catch (e) {
// Invalid URL, prevent navigation
event.preventDefault();
}
});
mainWindow.on("closed", () => {
mainWindow = null;
});
}
app.whenReady().then(() => {
// Set permissions for notifications and media
session.defaultSession.setPermissionRequestHandler(
(webContents, permission, callback) => {
const allowedPermissions = [
"notifications",
"media",
"mediaKeySystem",
"geolocation",
];
callback(allowedPermissions.includes(permission));
},
);
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
// Prevent multiple instances
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
} else {
app.on("second-instance", () => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
}