Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple JS to monitor offline and online time of a contact.

WhatsApp On Web Monitor

What It does

It gives notifications when someone goes online or offline or typing. Open chat of the contact you want to monitor and start script.

Simple Way

Tweet me @parthpower for bug reports or feature request. Special thanks to @jdcpower1994 for helping.

javascript:var csvContent="data:text/csv;charset=utf-8,Name,Status,Time,Date\n",globIntId=-1,buttonAdded=!1;function f(){try{b=document.querySelector("#main > header > div>div>span").textContent,name=document.querySelectorAll("#main > header > div>div>div>span")[1].textContent}catch(e){return}if("typing…"===b&&typing&&(typing=!1,checkPermission())){new Notification(name+" Typing");tm=new Date,csvContent+=name+",2,"+tm.toLocaleTimeString()+","+tm.toLocaleDateString()+"\n"}if(("online"===b||"typing…"===b)&&("online"===b&&(typing=!0),(!online||name!==lastName||ft)&&(online=!0,lastName=name,ft=!1,onlineTimeObj=new Date,onlineTimeMs=onlineTimeObj.getTime(),onlineTimeStr=onlineTimeObj.toLocaleString(),console.log(name+",1,"+onlineTimeObj.toLocaleTimeString()+","+onlineTimeObj.toLocaleDateString()+"\n"),csvContent+=name+",1,"+onlineTimeObj.toLocaleTimeString()+","+onlineTimeObj.toLocaleDateString()+"\n",checkPermission())))new Notification(name+" Online",{body:onlineTimeStr});if(-1<b.search("last seen")&&(online||ft||name!==lastName)&&(online=!1,ft=!1,lastName=name,typing=!0,offlineTimeObj=new Date,offlineTimeMs=offlineTimeObj.getTime(),offlineTimeStr=offlineTimeObj.toLocaleString(),console.log(name+",0,"+offlineTimeObj.toLocaleTimeString()+","+offlineTimeObj.toLocaleDateString()+"\n"),csvContent+=name+",0,"+offlineTimeObj.toLocaleTimeString()+","+offlineTimeObj.toLocaleDateString()+"\n",checkPermission()))new Notification(name+" Offline",{body:b})}function stalk(e){e=e||!1,ft=!0,online=!1,typing=!0,csvContent="data:text/csv;charset=utf-8,Name,Status,Time,Date\n",buttonAdded||(putCSVLink(),putStopStalkButton(),buttonAdded=!0);try{lastName=document.querySelector("#main > header > div.chat-body > div.chat-main > h2 > span")}catch(e){return}return!1!==e?(clearInterval(e),globIntId=-1,!0):(interval=setInterval(f,1e3),globIntId=interval,alert("Stalking"),interval)}function checkPermission(){return"Notification"in window?"granted"===Notification.permission||void("denied"!==Notification.permission&&Notification.requestPermission(function(e){if("granted"===e)return!0})):flase}function getCSV(e){e=encodeURI(e);var t=document.createElement("a");t.setAttribute("href",e),t.setAttribute("download","stalk_data.csv"),document.body.appendChild(t),t.click()}function getStalkData(){getCSV(csvContent)}function putCSVLink(){var e=document.createElement("button");e.textContent="GetStalkCSV",e.className="stalker",e.onclick=getStalkData,document.querySelector("#side > header").appendChild(e)}function toggleStalk(){-1===globIntId?(stalk(),this.textContent="Stop"):(clearInterval(globIntId),globIntId=-1,alert("Stalking Stopped"),this.textContent="Start",console.log("Stopped Stalking"))}function putStopStalkButton(){var e=document.createElement("button");e.className="stalker",e.textContent="ToggleStalk",e.onclick=toggleStalk,document.querySelector("#side > header").appendChild(e)}stalk();
//WhatsApp on Web stalker JS
//Copyright (c) 2016 Parh Parikh
//Distributed under MIT License
//Usage
//Open the chat you want to monitor
//stalk() to start stalking, return intervalId
//stalk(intervalId) to stop stalking
//Output is in console
//Quick Start
//Open Browser console, copy the code in console, write stalk()
//Happy stalking :)
var csvContent = "data:text/csv;charset=utf-8,Name,Status,Time,Date\n";
var globIntId = -1;
var buttonAdded = false;
function f() {
try {
b = document.querySelector('#main > header > div>div>span').textContent;
name = document.querySelectorAll('#main > header > div>div>div>span')[1].textContent;
} catch (TypeError) {
return;
}
if (b === "typing…" && typing) {
typing = false;
if (checkPermission()) {
var not = new Notification(name + " Typing");
tm = new Date();
csvContent += name + ",2," + tm.toLocaleTimeString() + "," + tm.toLocaleDateString() + "\n";
}
}
if (b === "online" || b === "typing…") {
if (b === "online") {
typing = true;
}
if (!online || name !== lastName || ft) {
online = true;
lastName = name;
ft = false;
onlineTimeObj = new Date();
onlineTimeMs = onlineTimeObj.getTime();
onlineTimeStr = onlineTimeObj.toLocaleString();
console.log(name + ",1," + onlineTimeObj.toLocaleTimeString() + "," + onlineTimeObj.toLocaleDateString() + "\n");
csvContent += name + ",1," + onlineTimeObj.toLocaleTimeString() + "," + onlineTimeObj.toLocaleDateString() + "\n";
if (checkPermission()) {
var not = new Notification(name + " Online",{
body: onlineTimeStr
});
}
}
}
if (b.search("last seen") > -1) {
if (online || ft || name !== lastName) {
online = false;
ft = false;
lastName = name;
typing = true;
offlineTimeObj = new Date();
offlineTimeMs = offlineTimeObj.getTime();
offlineTimeStr = offlineTimeObj.toLocaleString();
console.log(name + ",0," + offlineTimeObj.toLocaleTimeString() + "," + offlineTimeObj.toLocaleDateString() + "\n");
csvContent += name + ",0," + offlineTimeObj.toLocaleTimeString() + "," + offlineTimeObj.toLocaleDateString() + "\n";
if (checkPermission()) {
var not = new Notification(name + " Offline",{
body: b
});
}
}
}
}
function stalk(intervalId) {
intervalId = intervalId || false;
ft = true;
online = false;
typing = true;
csvContent = "data:text/csv;charset=utf-8,Name,Status,Time,Date\n";
if (!buttonAdded) {
putCSVLink();
putStopStalkButton();
buttonAdded = true;
}
try {
lastName = document.querySelector("#main > header > div.chat-body > div.chat-main > h2 > span");
} catch (TypeError) {
//stalk();
return;
}
if (intervalId !== false) {
clearInterval(intervalId);
globIntId = -1;
return true;
}
interval = setInterval(f, 1000);
globIntId = interval;
alert("Stalking");
//document.querySelector("#side > header > div.pane-list-user").textContent += "Stalking";
return interval;
}
//Modified Code from Notifiaction API example from MDN
function checkPermission() {
// Let's check if the browser supports notifications
if (!("Notification"in window)) {
return flase;
}// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
return true;
}// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function(permission) {
// If the user accepts, let's create a notification
if (permission === "granted") {
return true;
}
});
}
// At last, if the user has denied notifications, and you
// want to be respectful there is no need to bother them any more.
}
function getCSV(data) {
data = encodeURI(data);
var link = document.createElement("a");
link.setAttribute("href", data);
link.setAttribute("download", "stalk_data.csv");
document.body.appendChild(link);
link.click();
}
function getStalkData() {
getCSV(csvContent);
}
function putCSVLink() {
var button = document.createElement("button");
button.textContent = "GetStalkCSV";
button.className = "stalker";
button.onclick = getStalkData;
var sideBar = document.querySelector("#side > header");
sideBar.appendChild(button);
}
function toggleStalk() {
if (globIntId === -1) {
stalk();
this.textContent = "Stop";
} else {
clearInterval(globIntId);
globIntId = -1;
alert("Stalking Stopped");
this.textContent = "Start";
console.log("Stopped Stalking");
}
}
function putStopStalkButton() {
var button = document.createElement("button");
button.className = "stalker";
button.textContent = "ToggleStalk";
button.onclick = toggleStalk;
var sideBar = document.querySelector("#side > header");
sideBar.appendChild(button);
}
stalk();
@mxlng

This comment has been minimized.

Copy link

commented Apr 1, 2018

I don't know why, but no data was saved in my csv file.

@TheBloodyice

This comment has been minimized.

Copy link

commented Apr 28, 2018

Im to stupid can u do a video tutorial or add pictures on the tutorial in the description?

@porygon3000

This comment has been minimized.

Copy link

commented Apr 28, 2018

I'm also having the same blank CSV issue

@TheBloodyice You need to save the code under "Bookmarket" as a Bookmark in your browser, just copy the text, select to add a new bookmark and instead of an URL paste the copied code.

@parthpower

This comment has been minimized.

Copy link
Owner Author

commented Jul 18, 2018

Just updated the element selectors and now it works again.

@bendash

This comment has been minimized.

Copy link

commented Aug 1, 2018

Gives notification only once then again I have to press stop stalking and start and then again it gives notification once

@bendash

This comment has been minimized.

Copy link

commented Aug 1, 2018

It also does not give offline notification

@bendash

This comment has been minimized.

Copy link

commented Aug 1, 2018

It works if i keep switcvhing between the chats but gives only one notification if i opened asingle chat

@yiorenl

This comment has been minimized.

Copy link

commented Aug 20, 2018

Hi there, i'm facing the same issues as @bendash. Is there a workaround to solve that problem? It only sees the first time some is online. The other status are not recorded.

@philbarney

This comment has been minimized.

Copy link

commented Sep 8, 2018

Hi,
Great job, thanks a lot for this code. I slightly adapted it to also track when the status is changing from online to offline.

@troublie

This comment has been minimized.

Copy link

commented Sep 26, 2018

Tried twice right now but it seems to do nothing, I click on the bookmark and nothing happens while whatsappweb opened.

Is this not working?

@shahidcodes

This comment has been minimized.

Copy link

commented Sep 28, 2018

@philbarney could you gist your adapted code?

@ergoamox

This comment has been minimized.

Copy link

commented Jan 13, 2019

Hi there, i'm facing the same issues as @bendash. Is there a workaround to solve that problem? It only sees the first time some is online. The other status are not recorded.

This JS code works fine on Whatsapp with an english interface. Change your language from your smartphone to English. Otherwise the recording doesn't work.

@StefanSOD

This comment has been minimized.

Copy link

commented Feb 15, 2019

Hi there, i'm facing the same issues as @bendash. Is there a workaround to solve that problem? It only sees the first time some is online. The other status are not recorded.

This JS code works fine on Whatsapp with an english interface. Change your language from your smartphone to English. Otherwise the recording doesn't work.

What lines do we need to change in order to let this work, when using another language than the default (en)?

@StefanSOD

This comment has been minimized.

Copy link

commented Feb 15, 2019

I think I found a couple of bugs...

  • On line 13 you set the var cvsContent, which I think you overwrite in line 75 (function stalk());
    Why do we need that again?

  • Line 102 says "flase", where it should be "false";

Fact is, that there is never more logged than 1 event, although I'm pretty sure there are more events... What could I check?

please be gentle, I'm a noob when it comes to JS and programming. Still learning...

@StefanSOD

This comment has been minimized.

Copy link

commented Feb 21, 2019

@StefanSOD

This comment has been minimized.

Copy link

commented Feb 21, 2019

@schurik222

This comment has been minimized.

Copy link

commented Mar 25, 2019

I am too stupid...where can I find the file ?

@george5th

This comment has been minimized.

Copy link

commented Apr 20, 2019

First I open whatsapp in Firefox and open a chat, than I push F12 and copy the script in the console. I push ENTER and on screen I see "stalk". And now ??? What must I do know or ist it a mistake ? Please help me.

@gnncl

This comment has been minimized.

Copy link

commented May 9, 2019

The display language of your android or ios device must be in english.
Sample View

@Maynstream

This comment has been minimized.

Copy link

commented May 9, 2019

It's a pity you have to refresh to get multiple notifications. Now it only registers the first one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.