javascript WhatsApp在线状态

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript WhatsApp在线状态相关的知识,希望对你有一定的参考价值。

// 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);
}

以上是关于javascript WhatsApp在线状态的主要内容,如果未能解决你的问题,请参考以下文章

我可以使用.click功能切换WhatsApp Web中的用户选项卡吗?

有没有办法从桌面/服务器盒发送WhatsApp消息?

隐藏状态栏android时的白条一瞥(如何使它像Whatsapp状态)

javascript 添加'分享whatsapp'

javascript Comofazeralguémteresponder no whatsapp

javascript Comofazeralguémteresponder no whatsapp