Skip to content

Commit 6b7babe

Browse files
committed
Add automation scripts like WA bot
1 parent 9c8d7b5 commit 6b7babe

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

javascript/automation/whatsapp_bot.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const storeKey = 'WATracker';
2+
3+
// Select the node that will be observed for mutations
4+
const targetNode = document.querySelector('#main header');
5+
6+
// Options for the observer (which mutations to observe)
7+
const config = {
8+
attributes: true,
9+
childList: true,
10+
subtree: true
11+
};
12+
13+
// Callback function to execute when mutations are observed
14+
const callback = function(mutationsList, observer) {
15+
console.log('user status changed');
16+
17+
let name = document.querySelector('#main header span[dir="auto"]');
18+
let onlineStatus = document.querySelector('span[title="online"]');
19+
let username = name.innerText;
20+
let date = new Date().toISOString();
21+
let status = onlineStatus ? 'online' : 'offline';
22+
23+
let messageData = JSON.parse(localStorage.getItem(storeKey)) || {};
24+
25+
const found = messageData[username];
26+
27+
if (found) {
28+
messageData[username].push({ date, status });
29+
} else {
30+
messageData[username] = [{ date, status }];
31+
}
32+
33+
let json = JSON.stringify(messageData);
34+
35+
localStorage.setItem(storeKey, json);
36+
};
37+
38+
// Create an observer instance linked to the callback function
39+
const observer = new MutationObserver(callback);
40+
41+
// Start observing the target node for configured mutations
42+
observer.observe(targetNode, config);
43+
44+
// Later, you can stop observing
45+
// observer.disconnect();

0 commit comments

Comments
 (0)