WhatsApp Dual Timezone

Show message timestamps in two timezones.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        WhatsApp Dual Timezone
// @namespace   nikhilweee
// @match       https://web.whatsapp.com/*
// @grant       none
// @version     1.0
// @author      nikhilweee
// @description Show message timestamps in two timezones.
// ==/UserScript==

let lastChat = null;

function convertESTtoIST(time) {
  const localDate = new Date();
  const [time12h, period] = time.split(" ");
  const [hours, minutes] = time12h.split(":");
  localDate.setHours(period === "pm" ? +hours + 12 : +hours, +minutes);

  const estDate = localDate.toLocaleString("en-IN", {
    hour: "2-digit",
    minute: "2-digit",
    hour12: false,
    timeZone: "America/New_York",
  });
  const istDate = localDate.toLocaleString("en-IN", {
    hour: "2-digit",
    minute: "2-digit",
    hour12: false,
    timeZone: "Asia/Kolkata",
  });

  return `${estDate} EST | ${istDate} IST`;
}

function shouldUpdateTimestamps() {
  const header = document.querySelector("#main header");
  if (header === null) {
    return false;
  }
  if (header.innerText !== lastChat) {
    lastChat = header.innerText;
    return true;
  }
  return false;
}

function updateTimestamps() {
  document.querySelectorAll("span").forEach((span) => {
    if ([" am", " pm"].includes(span.innerHTML.slice(-3))) {
      span.innerHTML = convertESTtoIST(span.textContent);
    }
  });
}

function runScriptWhenReady() {
  if (shouldUpdateTimestamps()) {
    updateTimestamps();
  }
  setTimeout(runScriptWhenReady, 1000);
}

window.addEventListener("load", runScriptWhenReady);