forked from robyf/google-chat-linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrappedWindow.js
201 lines (172 loc) · 6.03 KB
/
wrappedWindow.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
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
'use strict';
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
const electron = require('electron');
var app = electron.app; // Module to control application life.
var BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
var ipc = electron.ipcMain;
var shell = electron.shell;
var Tray = electron.Tray;
var Menu = electron.Menu;
var localShortcut = require('electron-localshortcut'); // Module to register keyboard shortcuts
var appIcon = undefined;
var window;
const contextMenu = require('electron-context-menu');
contextMenu({ showInspectElement: false });
const ICON_NO_NEW_MSG = path.join(__dirname, 'assets/icon/chat-favicon-no-new-256dp.png');
const ICON_NEW_NON_NOTIF_MSG = path.join(__dirname, 'assets/icon/chat-favicon-new-non-notif-256dp.png');
const ICON_NEW_NOTIF_MSG = path.join(__dirname, 'assets/icon/chat-favicon-new-notif-256dp.png');
const ICON_OFFLINE_MSG = path.join(__dirname, 'assets/icon/chat-favicon-offline-256dp.png');
const HELPER_JS = path.join(__dirname, 'assets/js/helper.js');
module.exports = function createWrappedWindow(opts) {
// Thanks imskull! (https://github.com/atom/electron/issues/526#issuecomment-132942967)
// Try to load saved window bounds
var initPath = path.join(app.getPath("userData"), 'init.json');
var data;
try {
data = JSON.parse(fs.readFileSync(initPath, 'utf8'));
}
catch (e) { }
var sha = crypto.createHash('sha256');
sha.update(opts.name);
var hash = sha.digest('hex');
const iconPath = path.join(__dirname, 'assets/icon/chat-favicon-no-new-256dp.png');
// Create the browser window.
var windowOpts = (data && data[hash] && data[hash].bounds) ? data[hash].bounds : { width: 800, height: 600 };
windowOpts['auto-hide-menu-bar'] = true;
windowOpts['web-preferences'] = { 'node-integration': false };
windowOpts['icon'] = iconPath;
window = new BrowserWindow(windowOpts);
window.setMenu(null);
//window.webContents.openDevTools();
if (data && data[hash] && data[hash].shouldBeMaximized) {
window.maximize();
}
// and load the url ;)
window.loadURL(opts.url);
// Register common navigation shortcuts
function goBack() {
if (window.webContents.canGoBack()) {
window.webContents.goBack();
}
}
function goForward() {
if (window.webContents.canGoForward()) {
window.webContents.goForward();
}
}
localShortcut.register(window, 'Alt+Left', goBack);
localShortcut.register(window, 'Alt+Right', goForward);
localShortcut.register(window, 'F5', window.webContents.reload);
// Open EXTERNAL LINKS in the default browser
// Example: electron-wrap messenger.com
// Links on messenger.com that link to messenger.com will be followed in-app
// Links on messenger.com that link elsewhere (e.g., imgur.com) will be opened externally
// http://stackoverflow.com/a/23945027/5136076
function extractDomain(url) {
var domain;
//find & remove protocol (http, ftp, etc.) and get domain
if (url.indexOf("://") > -1) {
domain = url.split('/')[2];
}
else {
domain = url.split('/')[0];
}
//find & remove port number
domain = domain.split(':')[0];
return domain;
}
var handleRedirect = function (e, url) {
if (!opts.openLocally && extractDomain(url) !== extractDomain(window.webContents.getURL()) && extractDomain(url) !== "accounts.google.com" && extractDomain(url) !== "accounts.youtube.com" && extractDomain(url) !== "support.google.com" && extractDomain(url) !== "chat.google.com") {
require('electron').shell.openExternal(url);
e.preventDefault();
/*
} else {
var nested = createWrappedWindow({
name: opts.name,
url: url,
openLocally: opts.openLocally
});
nested.on('closed', function() {
nested = null;
});
*/
}
};
window.webContents.on('will-navigate', handleRedirect);
window.webContents.on('new-window', handleRedirect);
// Save the window bounds on close
window.on('close', function () {
var maximized = window.isMaximized();
var newData = data || {};
newData[hash] = { bounds: window.getBounds() };
if (window.isMaximized()) {
newData[hash].shouldBeMaximized = true;
}
fs.writeFileSync(initPath, JSON.stringify(newData));
});
window.webContents.on('dom-ready', () => {
const js = fs.readFileSync(HELPER_JS, 'utf8')
// const scriptPath = path.join('file://', __dirname, 'node_modules/jquery/dist/jquery.min.js');
window.webContents.executeJavaScript(js);
});
appIcon = new Tray(ICON_OFFLINE_MSG);
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show', click: function () {
window.show()
}
}, {
label: 'Hide', click: function () {
window.minimize()
}
}, {
label: 'Quit', click: function () {
app.isQuiting = true
app.quit()
}
}
]);
appIcon.setContextMenu(contextMenu);
appIcon.on('click', function (e) {
if (window.isMinimized()) {
window.show();
} else {
window.focus();
}
});
return window;
};
ipc.on('open-link', (evt, href) => {
shell.openExternal(href);
});
ipc.on('show', (evt, href) => {
window.show();
});
function iconForType(itype) {
if (itype == "NORMAL") {
return ICON_NO_NEW_MSG;
} else if (itype == "UNREAD") {
return ICON_NEW_NON_NOTIF_MSG;
} else if (itype == "ATTENTION") {
return ICON_NEW_NOTIF_MSG;
}
return ICON_OFFLINE_MSG;
}
ipc.on('favicon-changed', (evt, href) => {
/* console.log("Favicon changed: ", href); */
var itype = "";
if (href.match(/chat-favicon-no-new/)) {
itype = "NORMAL";
} else if (href.match(/chat-favicon-new-non-notif/)) {
itype = "UNREAD";
window.webContents.executeJavaScript('newMessage()');
} else if (href.match(/chat-favicon-new-notif/)) {
itype = "ATTENTION";
window.webContents.executeJavaScript('newMessage()');
} else if (href.match(/^data:image\/png;base64,iVBOR.+/)) {
itype = "OFFLINE";
}
appIcon.setImage(iconForType(itype));
});