Skip to content

Instantly share code, notes, and snippets.

@mbecker
Last active October 23, 2018 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbecker/cc1c2a98d4e5a965274adc87219e394d to your computer and use it in GitHub Desktop.
Save mbecker/cc1c2a98d4e5a965274adc87219e394d to your computer and use it in GitHub Desktop.
WhatsApp Online Status
// This script identifies how long a contact is online in the WhatsApp-Webapp
// The contact must have enabled to share his online stats
// 1. Login at the WhatsApp-Webapp: https://web.whatsapp.com/
// 2. Select the contact to watch at
// 3. Open in Chrome the web console via right click and "inspect" (or press 'strg+shift+j')
// 4. Open the 'console' and copy/paste the code; press enter at the end
// 5. Open the printed array in the console and see online status with duration in milliseconds
// The script inspect every x ms if the user is online;
// Test in jsfiddle: http://jsfiddle.net/mbecker/85Lekhtc/53/
var onlineTimes = []; // New array for online times
var secondsBetweenOnlineTimes = 10; // The duration how long a contact must be offline to count the online session
var millisecondsToCheckTheOnlineStatus = 10; // 1000ms = 1sec
var millisecondsToPrintTheOnlineStatus = 2000; // 10000ms = 10sec; print the list of online status to the console
window.setInterval(checkOnlineElement, millisecondsToCheckTheOnlineStatus);
window.setInterval(printOnlineTimes, millisecondsToPrintTheOnlineStatus);
// This function print the list of online times
function printOnlineTimes() {
console.log(onlineTimes);
}
//
var newOnlineStatus = true;
function checkOnlineElement() {
lastCheckTime = Date.now();
var el = document.querySelector('[title="online"]');
// Check that the element exists
if(el !== null) {
// The element exists; now check if it's a new or existing online session
checkOnlineSession(lastCheckTime);
newOnlineStatus = false;
} else {
// The element online does not exists, that means the next appearance is a new online stats
newOnlineStatus = true;
}
}
// Check if the online element appearance is completely new (array length === 0), or a new online status 'newOnlineStatus===true' or an existing online session
function checkOnlineSession (time) {
if(newOnlineStatus) {
var onlineObject = {
firstOnline: convertTimestamp(time),
lastOnline: 0,
durationInMilliSeconds: 0,
duration: 0
};
onlineTimes.push(onlineObject);
} else if(typeof onlineTimes[onlineTimes.length - 1].lastOnline !== "undefined") {
// Existing online session and current online session exists in array
onlineTimes[onlineTimes.length - 1].lastOnline = convertTimestamp(time)
onlineTimes[onlineTimes.length - 1].durationInMilliSeconds = onlineTimes[onlineTimes.length - 1].durationInMilliSeconds + millisecondsToCheckTheOnlineStatus
onlineTimes[onlineTimes.length - 1].duration = millisToMinutesAndSeconds(onlineTimes[onlineTimes.length - 1].durationInMilliSeconds);
}
}
// Convert the timestamp to european/german time - missed gist link from other source
function convertTimestamp(timestamp) {
var d = new Date(timestamp * 1), // Convert the passed timestamp to milliseconds
yyyy = d.getFullYear(),
mm = ('0' + (d.getMonth() + 1)).slice(-2), // Months are zero based. Add leading 0.
dd = ('0' + d.getDate()).slice(-2), // Add leading 0.
hh = d.getHours(),
min = ('0' + d.getMinutes()).slice(-2), // Add leading 0.
sec = ('0' + d.getSeconds()).slice(-2),
time;
// ie: 22.10.2018 - 19:06:12
time = dd + '.' + mm + '.' + yyyy + ' - ' + hh + ':' + min + ':' + sec;
return time;
}
// https://stackoverflow.com/questions/21294302/converting-milliseconds-to-minutes-and-seconds-with-javascript
function millisToMinutesAndSeconds(millis) {
var minutes = Math.floor(millis / 60000);
var seconds = ((millis % 60000) / 1000).toFixed(0);
return (seconds == 60 ? (minutes+1) + ":00" : minutes + ":" + (seconds < 10 ? "0" : "") + seconds);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment