forked from klaudiosinani/tusk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
585 lines (492 loc) Β· 14.9 KB
/
browser.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
'use strict';
const electron = require('electron');
const os = require('os');
const path = require('path');
const timeStamp = require('time-stamp');
const config = require('./config');
const join = path.join;
const ipc = electron.ipcRenderer;
const shell = electron.shell;
const webFrame = electron.webFrame;
const tuskJSON = '.tusk.json'; // Config file name
const homeDir = os.homedir();
const homeConfig = join(homeDir, tuskJSON); // Config file on home directory
ipc.on('new-note', () => {
// Create new note
document.querySelector('#gwt-debug-Sidebar-newNoteButton').click();
});
ipc.on('delete-note', () => {
// Delete note
document.querySelector('#gwt-debug-NoteAttributes-trashButton').click();
});
ipc.on('new-notebook', () => {
// Create new notebook
const notebooks = document.querySelector('#gwt-debug-Sidebar-notebooksButton');
notebooks.click();
document.querySelector('#gwt-debug-NotebooksDrawer-createNotebookButton').click();
notebooks.click();
});
ipc.on('new-tag', () => {
// Create new tag
const tags = document.querySelector('#gwt-debug-Sidebar-tagsButton');
tags.click();
document.querySelector('.focus-drawer-TagsDrawer-TagsDrawer-create-tag-icon').click();
tags.click();
});
ipc.on('add-shortcut', () => {
// Add shortcut
document.querySelector('#gwt-debug-NoteAttributes-shortcutButton').click();
});
ipc.on('set-reminder', () => {
// Set reminder
document.querySelector('#gwt-debug-NoteAttributes-reminderButton').click();
});
ipc.on('search', () => {
// Search notes
document.querySelector('#gwt-debug-Sidebar-searchButton').click();
});
ipc.on('focus-mode', () => {
// Toggle focus mode
document.querySelector('#gwt-debug-NoteAttributes-focusButton').click();
});
ipc.on('exit-focus-mode', () => {
// Exit focus mode
document.querySelector('#gwt-debug-NoteAttributes-doneButton').click();
});
function untoggleTheme(themeName, activateFunction) {
// Deactivate theme status if it is not already deactivated
switch (config.get(themeName)) {
case true:
config.set(themeName, false);
activateFunction();
break;
default:
break;
}
}
function darkMode() {
document.documentElement.classList.toggle('dark-mode', config.get('darkMode'));
}
function untoggleDark() {
// Untoggle the dark theme
untoggleTheme('darkMode', darkMode);
}
ipc.on('toggle-dark-mode', () => {
untoggleSepia();
untoggleBlack();
untoggleVibrant();
untoggleDarkVibrant();
// Toggle the dark theme
config.set('darkMode', !config.get('darkMode'));
darkMode();
});
function blackMode() {
document.documentElement.classList.toggle('black-mode', config.get('blackMode'));
}
function untoggleBlack() {
// Untoggle the black theme
untoggleTheme('blackMode', blackMode);
}
ipc.on('toggle-black-mode', () => {
untoggleDark();
untoggleSepia();
untoggleVibrant();
untoggleDarkVibrant();
// Toggle the black theme
config.set('blackMode', !config.get('blackMode'));
blackMode();
});
function sepiaMode() {
document.documentElement.classList.toggle('sepia-mode', config.get('sepiaMode'));
}
function untoggleSepia() {
// Untoggle the sepia theme
untoggleTheme('sepiaMode', sepiaMode);
}
ipc.on('toggle-sepia-mode', () => {
untoggleBlack();
untoggleDark();
untoggleVibrant();
untoggleDarkVibrant();
// Toggle the sepia theme
config.set('sepiaMode', !config.get('sepiaMode'));
sepiaMode();
});
function vibrantMode() {
document.documentElement.classList.toggle('vibrant-mode', config.get('vibrantMode'));
// Activate vibrant mode on main window
ipc.send('activate-vibrant');
// Make app background transparent
document.documentElement.style.backgroundColor = 'transparent';
}
function untoggleVibrant() {
// Untoggle the vibrant theme
untoggleTheme('vibrantMode', vibrantMode);
}
ipc.on('toggle-vibrant-mode', () => {
untoggleBlack();
untoggleDark();
untoggleSepia();
untoggleDarkVibrant();
// Toggle the vibrant theme
config.set('vibrantMode', !config.get('vibrantMode'));
vibrantMode();
});
function vibrantDarkMode() {
document.documentElement.classList.toggle('vibrant-dark-mode', config.get('vibrantDarkMode'));
// Activate dark vibrant mode on main window
ipc.send('activate-vibrant');
// Make app background transparent
document.documentElement.style.backgroundColor = 'transparent';
}
function untoggleDarkVibrant() {
// Untoggle the dark vibrant theme
untoggleTheme('vibrantDarkMode', vibrantDarkMode);
}
ipc.on('toggle-vibrant-dark-mode', () => {
untoggleBlack();
untoggleDark();
untoggleSepia();
untoggleVibrant();
// Toggle the dark vibrant theme
config.set('vibrantDarkMode', !config.get('vibrantDarkMode'));
vibrantDarkMode();
});
function autoNightMode() {
// Switch between light and dark themes based on daytime
const time = timeStamp('HHmm');
switch (time <= 1800 && time >= 600) {
case true:
// Switch to the light theme
untoggleDark();
untoggleBlack();
untoggleSepia();
untoggleVibrant();
untoggleDarkVibrant();
break;
case false:
// Switch to the dark theme
untoggleBlack();
untoggleSepia();
untoggleVibrant();
untoggleDarkVibrant();
config.set('darkMode', true);
darkMode();
break;
default:
break;
}
}
function untoggleAutoNightMode() {
// Untoggle the auto night mode
untoggleDark();
}
ipc.on('auto-night-mode', () => {
// Toggle on and off the auto night mode
if (config.get('autoNightMode')) {
autoNightMode();
} else {
untoggleAutoNightMode();
}
});
function toggleSideBar() {
// Hide side bar & adjust left margin
document.documentElement.classList.toggle('side-bar-hidden', config.get('sideBarHidden'));
if (process.platform === 'darwin') {
// Macos visual tweak
document.documentElement.classList.toggle('side-bar-hidden-macos', config.get('sideBarHidden'));
}
}
ipc.on('toggle-side-bar', () => {
// Toggle on and off the side bar
config.set('sideBarHidden', !config.get('sideBarHidden'));
toggleSideBar();
});
function toggleMenuBar() {
// Activate the menu bar on the main window
ipc.send('activate-menu-bar');
}
ipc.on('toggle-menu-bar', () => {
// Toggle on and off the menu bar
config.set('menuBarHidden', !config.get('menuBarHidden'));
toggleMenuBar();
});
function goToNote(key) {
const index = key;
selectNote(index);
}
const noteSelector = '.focus-NotesView-Note';
const notesList = '.NotesView-ScrollWindow > div';
const notesListSelector = '.NotesView-ScrollWindow';
const selectedNoteSelector = '.focus-NotesView-Note-selected';
function scroll(pixels, downwards) {
// Scroll upwards or downwards on note switch
const notesScrollbox = document.querySelector(notesListSelector);
if (downwards) {
notesScrollbox.scrollTop += pixels;
} else {
notesScrollbox.scrollTop -= pixels;
}
}
function selectNote(index) {
// Select the appropriate note based on given index
document.querySelector(notesList).children[index].firstChild.firstChild.click();
}
function goToNextNote() {
// Navigate to the next note
const index = getCurrentIndex();
const nextIndex = getNextIndex(index);
console.log('Next note index is: ' + nextIndex);
selectNote(nextIndex);
scroll(110, true);
}
function goToPreviewsNote() {
// Navigate to the previews note
const index = getCurrentIndex();
const previewsIndex = getPreviewsIndex(index);
console.log('Previews note index is: ' + previewsIndex);
selectNote(previewsIndex);
scroll(110, false);
}
// Calculate the index of the current note
function getCurrentIndex() {
let i;
let currentIndex; // Index of current note
let notesArray = []; // Array of notes
// Get the css meta of the currently selected note
const selectedNote = document.querySelector(selectedNoteSelector);
// Create an array of notes relative to the currently selected note
notesArray = document.querySelector(notesListSelector).querySelectorAll(noteSelector);
// Traverse the array and find the index of selected note
for (i = 0; i < notesArray.length; i++) {
if (notesArray[i] === selectedNote) {
// Increment the selected note index
// since selectNote() navigates to
// positive non-zero values only
currentIndex = i + 1;
console.log('The currently selected note has an index of: ' + currentIndex);
}
}
// Return the current note index
return currentIndex;
}
// Calculate the index of the next note
// relatively to the current note index
function getNextIndex(currentIndex) {
const nextIndex = currentIndex + 1; // Index value of next note
console.log('The next note will have an index of: ' + nextIndex);
return nextIndex;
}
// Calculate the index of the previews note
// relatively to the current note index
function getPreviewsIndex(currentIndex) {
const previewsIndex = currentIndex - 1; // Index value of previews note
console.log('The previews note will have an index of: ' + previewsIndex);
return previewsIndex;
}
function printToPDF() {
ipc.send('print-to-pdf');
}
ipc.on('print', printToPDF);
function exportAsPDF() {
ipc.send('export-as-pdf');
}
ipc.on('export', exportAsPDF);
function toggleAutoLaunch() {
// Decide whether or not the app should launch on login
const startup = require('./startup');
if (config.get('autoLaunch')) {
// Activate app launching
startup.activate();
} else {
// Deactivate app launching
startup.deactivate();
}
}
ipc.on('auto-launch', toggleAutoLaunch);
ipc.on('next-note', goToNextNote);
ipc.on('previous-note', goToPreviewsNote);
ipc.on('toggle-notebooks', () => {
// Toggle notebooks list
document.querySelector('#gwt-debug-Sidebar-notebooksButton').click();
});
ipc.on('toggle-tags', () => {
// Toggle tags list
document.querySelector('#gwt-debug-Sidebar-tagsButton').click();
});
ipc.on('shortcuts', () => {
// Toggle Shortcuts
document.querySelector('#gwt-debug-Sidebar-shortcutsButton').click();
});
ipc.on('return', () => {
// Return to Notes
document.querySelector('#gwt-debug-Sidebar-notesButton').click();
});
ipc.on('zoom-in', () => {
// Get zoom factor and increase it
const currentZoomFactor = webFrame.getZoomFactor();
const zoomFactor = currentZoomFactor + 0.05;
// Upper bound check
if (zoomFactor < 1.3) {
webFrame.setZoomFactor(zoomFactor);
config.set('zoomFactor', zoomFactor);
}
});
ipc.on('zoom-out', () => {
// Get zoom factor and decrease it
const currentZoomFactor = webFrame.getZoomFactor();
const zoomFactor = currentZoomFactor - 0.05;
// Lower bound check
if (zoomFactor > 0.7) {
webFrame.setZoomFactor(zoomFactor);
config.set('zoomFactor', zoomFactor);
}
});
ipc.on('zoom-reset', () => {
// Reset zoom factor
webFrame.setZoomFactor(1.0);
config.set('zoomFactor', 1.0);
});
ipc.on('settings', () => {
// Toggle Settings
shell.openExternal('https://www.evernote.com/Settings.action');
});
ipc.on('edit-shortcuts', () => {
// Toggle config file
shell.openExternal(homeConfig);
});
ipc.on('log-out', () => {
// Log out
document.querySelector('#gwt-debug-AccountMenu-avatar').click();
document.querySelector('#gwt-debug-AccountMenu-logout').click();
});
ipc.on('bold', () => {
// Bold text
document.querySelector('#gwt-debug-FormattingBar-boldButton').click();
});
ipc.on('italic', () => {
// Italic text
document.querySelector('#gwt-debug-FormattingBar-italicButton').click();
});
ipc.on('underline', () => {
// Underline text
document.querySelector('#gwt-debug-FormattingBar-underlineButton').click();
});
ipc.on('add-link', () => {
// Add link
document.querySelector('#gwt-debug-FormattingBar-linkButton').click();
});
ipc.on('attach-file', () => {
// Add link
document.querySelector('#gwt-debug-FormattingBar-linkButton').click();
});
ipc.on('insert-drive', () => {
// Add link
document.querySelector('#gwt-debug-FormattingBar-attachmentButton').click();
});
ipc.on('align-left', () => {
// Align text left
document.querySelector('#gwt-debug-EditorAlignDropdown-left').click();
});
ipc.on('align-center', () => {
// Align text center
document.querySelector('#gwt-debug-EditorAlignDropdown-center').click();
});
ipc.on('align-right', () => {
// Align text right
document.querySelector('#gwt-debug-EditorAlignDropdown-right').click();
});
ipc.on('indent', () => {
// Increase indentation
document.querySelector('#gwt-debug-FormattingBar-indentButton').click();
});
ipc.on('outdent', () => {
// Decrease indentation
document.querySelector('#gwt-debug-FormattingBar-outdentButton').click();
});
ipc.on('numbered', () => {
// Numbered list
document.querySelector('#gwt-debug-FormattingBar-listButton').click();
});
ipc.on('bulleted', () => {
// Bulleted list
document.querySelector('#gwt-debug-FormattingBar-bulletButton').click();
});
ipc.on('strikethrough', () => {
// Strikethrough text
document.querySelector('#gwt-debug-FormattingBar-strikeButton').click();
});
ipc.on('checkbox', () => {
// Toggle checkbox
document.querySelector('#gwt-debug-FormattingBar-checkboxButton').click();
});
ipc.on('code-block', () => {
// Toggle code-block
document.querySelector('#gwt-debug-FormattingBar-codeBlockButton').click();
});
ipc.on('subscript', () => {
// Subscript text
const formatMenu = document.querySelector('#gwt-debug-FormattingBar-overflowButton');
formatMenu.click();
document.querySelector('#gwt-debug-FormattingBar-subscriptButton').click();
});
ipc.on('superscript', () => {
// Superscript text
const formatMenu = document.querySelector('#gwt-debug-FormattingBar-overflowButton');
formatMenu.click();
document.querySelector('#gwt-debug-FormattingBar-superscriptButton').click();
});
ipc.on('remove-formatting', () => {
// Remove text formatting
document.querySelector('#gwt-debug-FormattingBar-noFormatButton').click();
});
ipc.on('horizontal-rule', () => {
// Insert horizontal rule
document.querySelector('#gwt-debug-FormattingBar-horizontalRuleButton').click();
});
document.addEventListener('keydown', event => {
let comboKey;
// OS check
if (process.platform === 'darwin') {
comboKey = event.metaKey;
} else {
comboKey = event.ctrlKey;
}
// Validity check
if (comboKey === false) {
return null;
}
// Parse as decimal
const givenNum = parseInt(event.key, 10);
// Get index
if (givenNum < 10 && givenNum > 0) {
goToNote(givenNum);
}
});
document.addEventListener('DOMContentLoaded', () => {
// Preserve zoom factor
const zoomFactor = config.get('zoomFactor');
webFrame.setZoomFactor(zoomFactor);
// Toggle auto night mode
if (config.get('autoNightMode')) {
autoNightMode();
}
// Toggle the side bar
toggleSideBar();
// Toggle the menu bar
toggleMenuBar();
// Toggle sepia mode
sepiaMode();
// Toggle black mode
blackMode();
// Toggle dark mode
darkMode();
// Toggle vibrant mode
vibrantMode();
// Toggle vibrant dark mode
vibrantDarkMode();
// Prevent white flashing screen on startup
if (!config.get('vibrantMode') && !config.get('vibrantDarkMode')) {
document.documentElement.style.backgroundColor = '#212121';
}
});