HWM_live_header_clock

Живые часы в шапке

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name            HWM_live_header_clock
// @author          Мифист
// @namespace       Мифист
// @version         1.1.2
// @description     Живые часы в шапке
// @match           https://www.heroeswm.ru/*
// @match           https://*.lordswm.com/*
// @exclude         */war.php*
// @exclude         */cgame.php*
// @run-at          document-end
// @grant           none
// @license         MIT
// @noframes
// ==/UserScript==

(function() {
  'use strict';

  const $ = document.querySelector.bind(document);
  const format = (num) => num > 9 ? num : `0${num}`;
  const split = (text) => (text.match(/\d+/g) || []).map(x => format(+x));

  const hwmNode = $('.rr + div') || $('a[href$="/player.html"]');

  if (!hwmNode) return;

  const timerNode = document.createTextNode('h:m:s');
  const hwmTime = [];

  if (location.pathname === '/roulette.php') {
    const elem = $('#roul_time') || { textContent: '' };
    hwmTime.push(...split(elem.textContent));
  }

  if (hwmNode.tagName === 'A') {
    const node = hwmNode.previousSibling;
    const data = node.data.trim();
    node.data = data.replace(/[^,]+/, '');
    hwmTime.push(...split(data).slice(0, -1));
    node.before(timerNode);
  } else {
    hwmTime.push(...split(hwmNode.textContent));
    hwmNode.replaceChildren(timerNode);
  }

  const date = new Date();
  const YY = date.getFullYear();
  const MM = format(date.getMonth() + 1);
  const DD = format(date.getDate());
  const [hh, mm, ss = '00'] = hwmTime;
  const dateString = `${YY}-${MM}-${DD}T${hh}:${mm}:${ss}`;
  const staticTime = Date.parse(dateString);
  const startTime = +date;

  (function loop() {
    if (timerNode.isConnected === false) return;

    const date = new Date(Date.now() - startTime + staticTime);
    const h = format(date.getHours());
    const m = format(date.getMinutes());
    const s = format(date.getSeconds());
    timerNode.data = `${h}:${m}:${s}`;
    setTimeout(loop, 1e3);
  })();
})();