Capturing chat content in Whatsapp Web

Simple Script to log chat history in Whatsapp Web.

Limitaion

  1. Can only monitor the active group
  2. Not working after changed active group, refresh and run the scripts again

Scripts

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
var gObserver = [];

var observeDOM = (function(){
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;

return function( obj, callback ){
if( !obj || !obj.nodeType === 1 ) return; // validation

if( MutationObserver ){
// define a new observer
var obs = new MutationObserver(function(mutations, observer){
gObserver.push(observer);
callback(mutations, observer);
})

// have the observer observe foo for changes in children
obs.observe( obj, { childList:true, subtree:true });
}

else if( window.addEventListener ){
obj.addEventListener('DOMNodeInserted', callback, false);
obj.addEventListener('DOMNodeRemoved', callback, false);
}
}
})();
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
observeDOM(document.getElementById("main").querySelector(".copyable-area div"), function(mutations, observer) {
console.debug(mutations);
for (var index in mutations) {
try {
if (mutations[index].target == undefined || mutations[index].target.tagName != "DIV") {
console.debug(mutations[index]);
break;
}
var message = mutations[index].addedNodes[0];
var newMessage = message.querySelector(".copyable-text");
var preText = newMessage.getAttribute("data-pre-plain-text")
var text = "";

newMessage.querySelectorAll(".copyable-text").forEach(function(key) {
text += key.innerText || key.getAttribute("data-plain-text") || "";
})
console.info("Adding: "+ preText + "\n"+ text);
} catch(er) {
try {
console.debug(er);
if (mutations[index].target == undefined || mutations[index].target.tagName != "DIV") {
console.debug(mutations[index]);
break;
}
if (mutations[index].addedNode == undefined) {
var message = mutations[index].removedNodes[0];
var newMessage = message.querySelector(".copyable-text");
var preText = newMessage.getAttribute("data-pre-plain-text")
var text = "";

newMessage.querySelectorAll(".copyable-text").forEach(function(key) {
text += key.innerText || key.getAttribute("data-plain-text");
})
console.info("Removeing: "+ preText + "\n"+ text);
}
} catch(err) {
console.debug(err);
}
}
}
})