-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWhatsApp Shortcuts.user.js
242 lines (195 loc) · 7.45 KB
/
WhatsApp Shortcuts.user.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
// ==UserScript==
// @name WhatsApp Shortcuts
// @namespace WA-Shortcuts
// @version 0.4.1
// @description Adding shortcuts to WhatsApp web application.
// @author lai32290
// @match https://web.whatsapp.com/
// @require https://cdnjs.cloudflare.com/ajax/libs/mousetrap/1.6.3/mousetrap.min.js
// @grant none
// ==/UserScript==
(function() {
'use strict';
let currentReply = null;
const WhatsApp = {
conversation : {
currentConversationIndex: 0,
totalConversationElements: 0,
},
findParent: function(el, sel) {
while ((el = el.parentElement) && !((el.matches || el.matchesSelector).call(el, sel)));
return el;
},
isLoading: function() {
const input = this.getSearchInput();
return input === null;
},
focusSearch: function() {
this.getSearchInput().focus();
},
getSearchInput: function() {
return document.querySelector('div._2S1VP');
},
getMessageInput: function() {
return document.querySelector("#main ._3F6QL > .selectable-text");
},
getPrevMessage() {
if (currentReply === null || this.findParent.call(this, currentReply, "body") === null) {
const messages = document.querySelectorAll(".vW7d1");
return messages[messages.length - 1];
}
return currentReply.previousSibling;
},
getNextMessage() {
if (currentReply === null || this.findParent.call(this, currentReply, "body") === null) {
const messages = document.querySelectorAll(".vW7d1");
return messages[messages.length - 1];
}
return currentReply.nextSibling;
},
getConversation(container) {
return container.querySelector('div._2EXPL');
},
getConversations() {
return this.getSidePanel().querySelectorAll('._2wP_Y');
},
getSortedConversations() {
function getConversationTop(el) {
return parseInt(el.style.transform.replace(/\D/g, ''));
}
const conversations = Array.apply(this, this.getConversations());
return conversations.sort((elA, elB) => {
const elATop = getConversationTop(elA);
const elBTop = getConversationTop(elB);
return elATop - elBTop;
});
},
getCurrentConversation() {
const conversations = Array.apply(this, this.getConversations());
return conversations.find(el => el.querySelector('div._2EXPL._1f1zm'));
},
getSidePanel() {
return document.querySelector('#pane-side');
},
getConversationByIndex(index) {
return this.getSortedConversations()[index];
}
}
function reactEventHandlers(element){
const reactHandlerKey = Object.keys(element).filter(function(item){
return item.indexOf('__reactEventHandlers')>=0
});
const reactHandler = element[reactHandlerKey[0]];
return reactHandler;
}
function bindChangeOfConversation(){
const event = new MouseEvent('onMouseDown', {
'view': window,
'bubbles': true,
'cancelable': true
});
Mousetrap.bind(['alt+[', 'command+['], function() {
try {
const activedConversation = WhatsApp.getCurrentConversation();
const sortedConversations = WhatsApp.getSortedConversations();
const nextIndex = sortedConversations.indexOf(activedConversation);
let nextConversationContainer = sortedConversations[ nextIndex + 1 ];
if(!nextConversationContainer) {
nextConversationContainer = WhatsApp.getSortedConversations()[0];
};
const nextConversation = WhatsApp.getConversation(nextConversationContainer);
const reactHandler = reactEventHandlers(nextConversation);
reactHandler.onMouseDown(event);
}
catch(err){
console.log(err);
}
});
Mousetrap.bind(['alt+]', 'command+]'], function() {
try{
const activedConversation = WhatsApp.getCurrentConversation();
const sortedConversations = WhatsApp.getSortedConversations();
const nextIndex = sortedConversations.indexOf(activedConversation);
let nextConversationContainer = sortedConversations[ nextIndex - 1 ];
if(!nextConversationContainer) {
const sortedConversations = WhatsApp.getSortedConversations();
nextConversationContainer = sortedConversations[ sortedConversations.length - 1];
};
const nextConversation = WhatsApp.getConversation(nextConversationContainer);
const reactHandler = reactEventHandlers(nextConversation);
reactHandler.onMouseDown(event);
}
catch(err){
console.log(err);
}
});
}
function doubleClick(selector, _element = null) {
const element = _element || document.querySelector(selector);
if (!element) return;
const event = new MouseEvent('dblclick', {
'view': window,
'bubbles': true,
'cancelable': true
});
element.dispatchEvent(event);
}
function click(selector, _element = null) {
const element = _element || document.querySelector(selector);
if (!element) return;
const event = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': true
});
element.dispatchEvent(event);
}
function bindSearch() {
let input;
while (input == null) {
input = WhatsApp.getSearchInput();
}
Mousetrap.bind(['ctrl+/', 'command+/'], function() {
WhatsApp.focusSearch();
});
}
function bindChangeConversation() {
Mousetrap.bind(['alt+up', 'alt+k', 'command+k', 'command+up'], function() {
const message = WhatsApp.getPrevMessage();
if (message) {
doubleClick("", message);
currentReply = message;
}
});
Mousetrap.bind(['alt+down', 'alt+j', 'command+j', 'command+down'], function() {
const message = WhatsApp.getNextMessage();
if (message) {
doubleClick("", message);
currentReply = message;
}
});
document.addEventListener("keyup", function(e) {
if (e.target.classList.contains("_2S1VP")) {
if (e.keyCode === 27 || e.keyCode === 13) {
currentReply = null;
}
}
});
}
function start() {
if (WhatsApp.isLoading()) {
setTimeout(start, 200);
return;
}
bindSearch();
bindChangeConversation();
bindChangeOfConversation();
document.addEventListener("keydown", function(e) {
if (e.target.classList.contains("selectable-text")) {
const input = WhatsApp.getMessageInput();
input.classList.add("mousetrap");
}
});
}
start();
})();