forked from antoineguilbert/android-messages-for-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
146 lines (126 loc) · 3.55 KB
/
main.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
//Constants
const electron = require('electron');
const path = require('path');
const url = require('url');
const windowStateKeeper = require('electron-window-state');
const {app, BrowserWindow, Menu, MenuItem} = electron;
let mainWindow
//Creating the window
function createWindow () {
let mainWindowState = windowStateKeeper({
defaultWidth: 1000,
defaultHeight: 720
});
const windowOptions = {
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
title: app.getName(),
icon: path.join(__dirname, '/icons/png/512.png')
}
mainWindow = new BrowserWindow(windowOptions)
//mainWindow.webContents.openDevTools();
mainWindow.loadURL(url.format({
pathname: path.join('messages.android.com'),
protocol: 'https:',
slashes: true
}))
//When a SMS arrived in the app, change the badge
if (process.platform === 'darwin') {
mainWindow.on('page-title-updated', function (e, title) {
var messages = countMessages(title);
if (messages) {
app.dock.setBadge(messages);
} else {
app.dock.setBadge('');
}
});
}
mainWindow.on('closed', () => {
mainWindow = null
});
mainWindowState.manage(mainWindow);
}
//Creating the menu
function createMenu (){
var i18n = new(require('./translations/i18n'));
const template = [
{
label: i18n.__('Edit'),
submenu: [
{label: i18n.__('Copy'),role: 'copy'},
{label: i18n.__('Paste'),role: 'paste'},
{label: i18n.__('Select all'),role: 'selectall'},
{type: 'separator'},
{label: i18n.__('Reload'),accelerator: 'CmdOrCtrl+R',click (item, focusedWindow) {if (focusedWindow) focusedWindow.reload()}},
]
},
{
label: i18n.__('Window'),
role: 'window'
}
]
if (process.platform === 'darwin') {
const name = app.getName()
template.unshift({
label: name,
submenu: [
{label: i18n.__('About'),role: 'about'},
{type: 'separator'},
{label: i18n.__('Disconnect account'), click: function click() {clearAppCache(); }},
{type: 'separator'},
{label: i18n.__('Hide')+' '+name,role: 'hide'},
{label: i18n.__('Hide others'),role: 'hideothers'},
{label: i18n.__('Unhide'),role: 'unhide'},
{type: 'separator'},
{label: i18n.__('Quit'),role: 'quit'}
]
})
template[1].submenu.push()
template[2].submenu = [
{label: i18n.__('Minimize'),accelerator: 'CmdOrCtrl+M',role: 'minimize'},
{label: i18n.__('Zoom'),role: 'zoom'}
]
}
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
//Function to clear the cache app
function clearAppCache() {
var session = mainWindow.webContents.session;
session.clearStorageData(function () {
session.clearCache(function () {
mainWindow.loadURL(url.format({
pathname: path.join('messages.android.com'),
protocol: 'https:',
slashes: true
}))
});
});
};
//Fonction to get the number of notifications
function countMessages(title) {
var itemCountRegex = /[([{]([\d.,]*)\+?[}\])]/;
var match = itemCountRegex.exec(title);
return match ? match[1] : undefined;
}
//When the app is ready
app.on('ready', function(){
createWindow();
if (process.platform === 'darwin') {
createMenu();
}
});
//Full closure of the app
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
///Creating a new window
app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
})