-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.js
125 lines (104 loc) · 3.04 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
'use strict';
const path = require('path');
const fs = require('fs');
const {app, BrowserWindow, shell, Tray, Menu} = require('electron');
const appMenu = require('./menu');
const configStore = require('./config');
let mainWindow;
let appIcon;
function updateBadge(title) {
const isOSX = Boolean(app.dock);
const messageCount = (/\(([0-9]+)\)/).exec(title);
if (isOSX) {
app.dock.setBadge(messageCount ? messageCount[1] : '');
if (messageCount) {
app.dock.bounce('informational');
}
}
if (messageCount) {
appIcon.setImage(path.join(__dirname, 'media', 'logo-tray-blue.png'));
} else {
appIcon.setImage(path.join(__dirname, 'media', 'logo-tray.png'));
}
}
function createMainWindow() {
const windowStateKeeper = require('electron-window-state');
const mainWindowState = windowStateKeeper({
defaultWidth: 1000,
defaultHeight: 800
});
const win = new BrowserWindow({
title: app.getName(),
show: false,
icon: process.platform === 'linux' && path.join(__dirname, 'media', 'logo.png'),
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
minWidth: 400,
minHeight: 200,
webPreferences: {
preload: path.join(__dirname, 'browser.js'),
nodeIntegration: false,
webSecurity: false,
plugins: true
}
});
mainWindowState.manage(win);
win.loadURL('https://web.whatsapp.com', {
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36'
});
win.on('closed', () => app.quit);
win.on('page-title-updated', (e, title) => updateBadge(title));
win.on('close', e => {
if (process.platform === 'darwin' && !win.forceClose) {
e.preventDefault();
win.hide();
} else if (process.platform === 'win32' && configStore.get('closeToTray')) {
win.hide();
e.preventDefault();
}
});
win.on('minimize', () => {
if (process.platform === 'win32' && configStore.get('minimizeToTray')) {
win.hide();
}
});
return win;
}
function createTray() {
appIcon = new Tray(path.join(__dirname, 'media', 'logo-tray.png'));
appIcon.setPressedImage(path.join(__dirname, 'media', 'logo-tray-white.png'));
appIcon.setContextMenu(appMenu.trayMenu);
appIcon.on('double-click', () => {
mainWindow.show();
});
}
app.on('ready', () => {
Menu.setApplicationMenu(appMenu.mainMenu);
mainWindow = createMainWindow();
createTray();
const page = mainWindow.webContents;
page.on('dom-ready', () => {
page.insertCSS(fs.readFileSync(path.join(__dirname, 'theme.css'), 'utf8'));
mainWindow.show();
});
page.on('new-window', (e, url) => {
e.preventDefault();
shell.openExternal(url);
});
page.on('did-finish-load', () => {
mainWindow.setTitle(app.getName());
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('before-quit', () => {
mainWindow.forceClose = true;
});
app.on('activate', () => {
mainWindow.show();
});