forked from klaudiosinani/tusk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
269 lines (238 loc) Β· 7.62 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
'use strict';
const path = require('path');
const fs = require('fs');
const electron = require('electron');
const os = require('os');
const isDevMode = require('electron-is-dev');
const ms = require('ms');
const timeStamp = require('time-stamp');
const decodeUri = require('decode-uri-component');
const appMenu = require('./menu');
const tray = require('./tray');
const config = require('./config');
const update = require('./update');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const ipcMain = electron.ipcMain;
const shell = electron.shell;
const dialog = electron.dialog;
require('electron-debug')({enabled: true});
require('electron-dl')();
require('electron-context-menu')();
let mainWindow;
let exiting = false;
const yinxiangURL = 'https://app.yinxiang.com/Login.action';
const evernoteURL = 'https://www.evernote.com/Login.action';
const functioning = app.makeSingleInstance(() => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.show();
}
});
if (functioning) {
app.quit();
}
function createMainWindow() {
const lastWindowState = config.get('lastWindowState');
const maxWindowInteger = 2147483647;
const darkModeFlag = config.get('darkMode') || config.get('blackMode');
const lastURL = config.get('useYinxiang') ? yinxiangURL : evernoteURL;
const tuskWindow = new electron.BrowserWindow({
title: app.getName(),
x: lastWindowState.x,
y: lastWindowState.y,
width: lastWindowState.width,
height: lastWindowState.height,
minWidth: 400,
minHeight: 200,
icon: process.platform === 'linux' && path.join(__dirname, 'static/Icon.png'),
alwaysOnTop: config.get('alwaysOnTop'),
titleBarStyle: 'hiddenInset',
darkTheme: darkModeFlag,
autoHideMenuBar: true,
show: false,
webPreferences: {
preload: path.join(__dirname, 'browser.js'),
nodeIntegration: false,
plugins: true
}
});
tuskWindow.loadURL(lastURL);
tuskWindow.on('close', e => {
if (!exiting) {
e.preventDefault();
if (process.platform === 'darwin') {
app.hide();
} else {
tuskWindow.hide();
}
}
});
tuskWindow.on('enter-full-screen', () => {
tuskWindow.setMaximumSize(maxWindowInteger, maxWindowInteger);
});
tuskWindow.on('page-title-updated', e => {
e.preventDefault();
});
tuskWindow.on('unresponsive', e => {
console.log('Unresponsive Tusk window. ', e);
});
tuskWindow.webContents.on('did-navigate-in-page', (e, url) => {
config.set('lastURL', url);
});
return tuskWindow;
}
app.on('ready', () => {
electron.Menu.setApplicationMenu(appMenu);
mainWindow = createMainWindow();
if (!config.get('hideTray')) {
// Check whether or not the tray
// icon is set to be hidden
tray.create(mainWindow);
}
const windowContent = mainWindow.webContents;
windowContent.on('dom-ready', () => {
windowContent.insertCSS(fs.readFileSync(path.join(__dirname, 'style/browser.css'), 'utf8'));
windowContent.insertCSS(fs.readFileSync(path.join(__dirname, 'style/dark-mode.css'), 'utf8'));
windowContent.insertCSS(fs.readFileSync(path.join(__dirname, 'style/black-mode.css'), 'utf8'));
windowContent.insertCSS(fs.readFileSync(path.join(__dirname, 'style/sepia-mode.css'), 'utf8'));
windowContent.insertCSS(fs.readFileSync(path.join(__dirname, 'style/vibrant-mode.css'), 'utf8'));
windowContent.insertCSS(fs.readFileSync(path.join(__dirname, 'style/vibrant-dark-mode.css'), 'utf8'));
if (config.get('launchMinimized')) {
// Check whether to launch the main window minimized
mainWindow.minimize();
} else {
mainWindow.show();
}
});
windowContent.on('new-window', (e, url) => {
e.preventDefault();
// Workaround for opening external links
const prefix = 'https://www.evernote.com/OutboundRedirect.action?dest=';
// Remove prefix & decode URL
url = decodeUri(url.replace(prefix, ''));
electron.shell.openExternal(url);
});
windowContent.on('crashed', e => {
console.log('Tusk window crashed. ', e);
});
update.init(electron.Menu.getApplicationMenu());
if (!isDevMode) {
setTimeout(() => {
update.checkUpdate();
}, ms('2m'));
setInterval(() => {
update.checkUpdate();
}, ms('1h'));
}
});
ipcMain.on('activate-vibrant', () => {
// Check if the vibrant theme was activated
if (config.get('vibrantMode')) {
// Set the app's background vibrant light
mainWindow.setVibrancy('light');
} else if (config.get('vibrantDarkMode')) {
// Set the app's background vibrant ultra dark
mainWindow.setVibrancy('ultra-dark');
} else {
// Remove background vibrancy
mainWindow.setVibrancy(null);
}
});
ipcMain.on('activate-menu-bar', () => {
// Check if the menu bar was activated
if (config.get('menuBarHidden')) {
// Hide the menu bar
mainWindow.setMenuBarVisibility(false);
// Restore ALT key toggling
mainWindow.setAutoHideMenuBar(true);
} else {
// Make the menu bar persistently visible
mainWindow.setMenuBarVisibility(true);
// Disable ALT key toggling
mainWindow.setAutoHideMenuBar(false);
}
});
ipcMain.on('print-to-pdf', event => {
// Get the current date-time
const dateTime = timeStamp('YYYY-MM-DD_HH-mm-ss');
const tmpDir = os.tmpdir();
// Construct the filename using the current date-time
const fileName = 'Tusk_Note_' + dateTime + '.pdf';
console.log('Date - time: ' + dateTime);
console.log('Temp directory: ' + tmpDir);
console.log('File to be printed: ' + fileName);
// Path of the file to be printed
const filePath = path.join(tmpDir, fileName);
// Get the window with the note that is to be printed
const noteWindow = BrowserWindow.fromWebContents(event.sender);
// Intialize printing process
noteWindow.webContents.printToPDF({}, (error, data) => {
if (error) {
return console.log(error.message);
}
// Write the pdf file to file path
fs.writeFile(filePath, data, err => {
if (err) {
return console.log(err.message);
}
// Open the pdf file to be printed
shell.openExternal('file://' + filePath);
});
});
});
ipcMain.on('export-as-pdf', event => {
// String to be removed from note title
const removeString = ' | Evernote Web';
// Get the note title
const noteTitle = mainWindow.webContents.getTitle().replace(removeString, '');
console.log('Note to be exported is titled: ' + noteTitle);
const noteWindow = BrowserWindow.fromWebContents(event.sender);
// `Save note` dialog options
const options = {
// Suggest note title as filename
defaultPath: noteTitle,
filters: [{
name: 'PDF File',
extensions: ['pdf']
}, {
name: 'All Files',
extensions: ['*']
}]
};
// Intialize printing process
noteWindow.webContents.printToPDF({}, (error, data) => {
if (error) {
return console.log(error.message);
}
// Set out the `Save note` dialog
dialog.showSaveDialog(options, fileName => {
// Check if the filepath is valid
if (fileName === undefined) {
return console.log('Note was not exported');
}
// Intialize the file writing process
fs.writeFile(fileName, data, err => {
if (err) {
dialog.showErrorBox('Exporting note error', err.message);
return console.log(err.message);
}
});
});
});
});
process.on('uncaughtException', error => {
// Report uncaught exceptions
console.log(error);
});
app.on('activate', () => {
mainWindow.show();
});
app.on('before-quit', () => {
exiting = true;
if (!mainWindow.isFullScreen()) {
config.set('lastWindowState', mainWindow.getBounds());
}
});