Initial commit
This commit is contained in:
commit
78e64c836f
13 files changed
+3774
No files matched your search
@@ -0,0 +1,135 @@
|
||||
|
||||
const {
|
||||
app,
|
||||
BrowserWindow,
|
||||
shell,
|
||||
session
|
||||
} = require('electron');
|
||||
|
||||
const path = require('node:path');
|
||||
|
||||
const WHATSAPP_URL = 'https://web.whatsapp.com/';
|
||||
const PARTITION = 'persist:whatsapp';
|
||||
|
||||
function isWhatsAppUrl(rawUrl) {
|
||||
try {
|
||||
const url = new URL(rawUrl);
|
||||
|
||||
return (
|
||||
url.protocol === 'https:' &&
|
||||
url.hostname === 'web.whatsapp.com'
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function isExternalUrl(rawUrl) {
|
||||
try {
|
||||
const url = new URL(rawUrl);
|
||||
return url.protocol === 'https:';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function createWindow() {
|
||||
const win = new BrowserWindow({
|
||||
width: 1200,
|
||||
height: 850,
|
||||
minWidth: 750,
|
||||
minHeight: 550,
|
||||
title: 'WhatsApp Desktop',
|
||||
icon: path.join(__dirname, 'build', 'icon.png'),
|
||||
autoHideMenuBar: true,
|
||||
backgroundColor: '#111b21',
|
||||
webPreferences: {
|
||||
partition: PARTITION,
|
||||
nodeIntegration: false,
|
||||
contextIsolation: true,
|
||||
sandbox: true,
|
||||
webSecurity: true,
|
||||
devTools: false
|
||||
}
|
||||
});
|
||||
|
||||
// Identify as Chromium rather than Electron.
|
||||
const chromeVersion = process.versions.chrome;
|
||||
|
||||
win.webContents.setUserAgent(
|
||||
`Mozilla/5.0 (X11; Linux x86_64) ` +
|
||||
`AppleWebKit/537.36 (KHTML, like Gecko) ` +
|
||||
`Chrome/${chromeVersion} Safari/537.36`
|
||||
);
|
||||
|
||||
// Block navigation outside WhatsApp Web.
|
||||
win.webContents.on('will-navigate', (event, url) => {
|
||||
if (!isWhatsAppUrl(url)) {
|
||||
event.preventDefault();
|
||||
|
||||
if (isExternalUrl(url)) {
|
||||
shell.openExternal(url);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Prevent new Electron windows.
|
||||
win.webContents.setWindowOpenHandler(({ url }) => {
|
||||
if (isExternalUrl(url) && !isWhatsAppUrl(url)) {
|
||||
shell.openExternal(url);
|
||||
}
|
||||
|
||||
return { action: 'deny' };
|
||||
});
|
||||
|
||||
// Disable downloads by default.
|
||||
win.webContents.session.on('will-download', (event) => {
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
// Load WhatsApp Web.
|
||||
win.loadURL(WHATSAPP_URL);
|
||||
|
||||
return win;
|
||||
}
|
||||
|
||||
app.whenReady().then(() => {
|
||||
const whatsappSession = session.fromPartition(PARTITION);
|
||||
|
||||
// Only allow notifications from WhatsApp Web.
|
||||
whatsappSession.setPermissionRequestHandler(
|
||||
(_webContents, permission, callback, details) => {
|
||||
const trusted = isWhatsAppUrl(
|
||||
details.requestingUrl || ''
|
||||
);
|
||||
|
||||
callback(
|
||||
trusted &&
|
||||
permission === 'notifications'
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
whatsappSession.setPermissionCheckHandler(
|
||||
(_webContents, permission, requestingOrigin) => {
|
||||
return (
|
||||
permission === 'notifications' &&
|
||||
isWhatsAppUrl(requestingOrigin)
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
createWindow();
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
createWindow();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user