Greasy Fork is available in English.

HWM_live_header_clock

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

  1. // ==UserScript==
  2. // @name HWM_live_header_clock
  3. // @author Мифист
  4. // @namespace Мифист
  5. // @version 1.1.2
  6. // @description Живые часы в шапке
  7. // @match https://www.heroeswm.ru/*
  8. // @match https://*.lordswm.com/*
  9. // @exclude */war.php*
  10. // @exclude */cgame.php*
  11. // @run-at document-end
  12. // @grant none
  13. // @license MIT
  14. // @noframes
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19.  
  20. const $ = document.querySelector.bind(document);
  21. const format = (num) => num > 9 ? num : `0${num}`;
  22. const split = (text) => (text.match(/\d+/g) || []).map(x => format(+x));
  23.  
  24. const hwmNode = $('.rr + div') || $('a[href$="/player.html"]');
  25.  
  26. if (!hwmNode) return;
  27.  
  28. const timerNode = document.createTextNode('h:m:s');
  29. const hwmTime = [];
  30.  
  31. if (location.pathname === '/roulette.php') {
  32. const elem = $('#roul_time') || { textContent: '' };
  33. hwmTime.push(...split(elem.textContent));
  34. }
  35.  
  36. if (hwmNode.tagName === 'A') {
  37. const node = hwmNode.previousSibling;
  38. const data = node.data.trim();
  39. node.data = data.replace(/[^,]+/, '');
  40. hwmTime.push(...split(data).slice(0, -1));
  41. node.before(timerNode);
  42. } else {
  43. hwmTime.push(...split(hwmNode.textContent));
  44. hwmNode.replaceChildren(timerNode);
  45. }
  46.  
  47. const date = new Date();
  48. const YY = date.getFullYear();
  49. const MM = format(date.getMonth() + 1);
  50. const DD = format(date.getDate());
  51. const [hh, mm, ss = '00'] = hwmTime;
  52. const dateString = `${YY}-${MM}-${DD}T${hh}:${mm}:${ss}`;
  53. const staticTime = Date.parse(dateString);
  54. const startTime = +date;
  55.  
  56. (function loop() {
  57. if (timerNode.isConnected === false) return;
  58.  
  59. const date = new Date(Date.now() - startTime + staticTime);
  60. const h = format(date.getHours());
  61. const m = format(date.getMinutes());
  62. const s = format(date.getSeconds());
  63. timerNode.data = `${h}:${m}:${s}`;
  64. setTimeout(loop, 1e3);
  65. })();
  66. })();