-
-
Notifications
You must be signed in to change notification settings - Fork 550
/
browser.js
90 lines (70 loc) · 2.43 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
'use strict';
const electron = require('electron');
const ipc = electron.ipcRenderer;
const app = electron.remote.app;
const storage = electron.remote.require('./storage');
const listSelector = 'div[role="navigation"] > ul > li';
ipc.on('show-preferences', () => {
// create the menu for the below
document.querySelector('._30yy._2fug._p').click();
document.querySelector('._54nq._2i-c._558b._2n_z li:first-child a').click();
});
ipc.on('new-conversation', () => {
document.querySelector('._30yy[href=\'/new\']').click();
});
ipc.on('log-out', () => {
// create the menu for the below
document.querySelector('._30yy._2fug._p').click();
document.querySelector('._54nq._2i-c._558b._2n_z li:last-child a').click();
});
ipc.on('find', () => {
document.querySelector('._58al').focus();
});
ipc.on('next-conversation', nextConversation);
ipc.on('previous-conversation', previousConversation);
ipc.on('dark-mode', toggleDarkMode);
app.on('platform-theme-changed', () => {
storage.set('darkMode', app.isDarkMode());
setDarkMode();
});
function nextConversation() {
const index = getNextIndex(true);
document.querySelectorAll(listSelector)[index].firstChild.firstChild.click();
}
function previousConversation() {
const index = getNextIndex(false);
document.querySelectorAll(listSelector)[index].firstChild.firstChild.click();
}
function setDarkMode() {
document.documentElement.classList.toggle('darkMode', storage.get('darkMode'));
}
function toggleDarkMode() {
storage.set('darkMode', !storage.get('darkMode'));
setDarkMode();
}
// return the index for next node if next is true,
// else returns index for the previous node
function getNextIndex(next) {
const selected = document.querySelector('._5l-3._1ht1._1ht2');
const list = Array.from(selected.parentNode.children);
const index = list.indexOf(selected) + (next ? 1 : -1);
return (index % list.length + list.length) % list.length;
}
// link the theme if it was changed while the app was closed
if (process.platform === 'darwin') {
storage.set('darkMode', app.isDarkMode());
}
// activate Dark Mode if it was set before quitting
setDarkMode();
// it's not possible to add multiple accelerators
// so need to do this the oldschool way
document.addEventListener('keydown', event => {
if (process.platform === 'darwin' && event.metaKey && event.shiftKey) {
if (event.keyCode === 221/* ] */) {
nextConversation();
}
if (event.keyCode === 219/* [ */) {
previousConversation();
}
}
});