HWM_live_header_clock

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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