-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (58 loc) · 2.32 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
(function monitorWhatsApp() {
console.log("Monitor Started...");
// List of messages to react
// when someone sends a message that includes any of these words
// in the current chat, the script will react replying with the reaction
const reactTo = ["+1", "+ 1"];
// The reaction to send. We accept emojis too!!
const reaction = "+1 ⚽️";
// This variable will help us to avoid sending the reaction multiple times
let reacted = false;
// Check for new messages every 2 second
const monitor = setInterval(checkLastMessage, 2 * 1000);
function sendMessage(text) {
// Find the message input box
const messageBox = document.querySelector(
'footer div[contenteditable="true"]'
);
if (messageBox) {
// Focus on the input box
messageBox.focus();
// We need to create a new event to trigger the input event
// This is necessary because WhatsApp uses React
// and the input event is not triggered by simply
// setting the value of the input box
const event = new InputEvent("input", {
bubbles: true,
cancelable: true,
data: text,
inputType: "insertText",
});
messageBox.dispatchEvent(event);
// Wait a moment and then send the message
setTimeout(() => {
const sendButton = document.querySelector(
'span[data-icon="send"]'
);
if (sendButton) sendButton.click();
// Let's free the monitor
clearInterval(monitor);
}, 200);
}
}
// This function will check the last message JUST in the current chat
function checkLastMessage() {
const messages = document.querySelectorAll(
"div.message-in span.selectable-text"
);
if (messages.length > 0) {
const lastMessage = messages[messages.length - 1].innerText.trim();
// If the last message is in the reactTo array
// and we haven't reacted to it yet
if (reactTo.some(item => lastMessage.includes(item)) && !reacted) {
sendMessage(reaction);
reacted = true;
}
}
}
})();