Little Doll Lite

Автоматическая смена и установка часового пояса

目前为 2024-02-11 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Little Doll Lite
  3. // @namespace Little Doll Lite
  4. // @version 1.0
  5. // @description Автоматическая смена и установка часового пояса
  6. // @author Maesta_Nequitia
  7. // @match *://2ch.hk/*
  8. // @grant GM_addStyle
  9. // @icon https://2ch.hk/favicon.ico
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16. // Получаем текущее время пользователя
  17. const userTime = new Date();
  18.  
  19. // Получаем московское время
  20. const moscowTime = new Date().toLocaleString('en-US', {timeZone: 'Europe/Moscow'});
  21. const moscowTimeObj = new Date(moscowTime);
  22.  
  23. // Вычисляем разницу во времени между московским временем и временем пользователя в миллисекундах
  24. const timeDifference = userTime - moscowTimeObj;
  25.  
  26. const daysOfWeek = ['Вск', 'Пнд', 'Втр', 'Срд', 'Чтв', 'Птн', 'Суб'];
  27.  
  28. const formatDate = (date) => {
  29. const dayOfWeek = daysOfWeek[date.getDay()];
  30. return `${date.getDate().toString().padStart(2, '0')}/${(date.getMonth() + 1).toString().padStart(2, '0')}/${date.getFullYear().toString().slice(-2)} ${dayOfWeek} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
  31. };
  32.  
  33. const modifyTime = (element) => {
  34. const [datePart, , timePart] = element.textContent.split(' ');
  35. const [day, month, year] = datePart.split('/');
  36. const [hours, minutes, seconds] = timePart.split(':');
  37.  
  38. const originalDateObject = new Date(`20${year}`, month - 1, day, hours, minutes, seconds);
  39.  
  40. // Применяем разницу во времени к времени на форуме
  41. const newDateObject = new Date(originalDateObject.getTime() + timeDifference);
  42.  
  43. element.textContent = formatDate(newDateObject);
  44. };
  45.  
  46. const handleMutations = (mutationsList) => {
  47. mutationsList.forEach((mutation) => {
  48. if (mutation.type === 'childList') {
  49. Array.from(mutation.addedNodes)
  50. .filter(node => node instanceof Element)
  51. .forEach(node => {
  52. Array.from(node.getElementsByClassName('post__time')).forEach(modifyTime);
  53. });
  54. }
  55. });
  56. };
  57.  
  58. const timeElements = document.querySelectorAll('.post__time');
  59. timeElements.forEach(modifyTime);
  60.  
  61. const observer = new MutationObserver(handleMutations);
  62. observer.observe(document.body, { childList: true, subtree: true });
  63.  
  64. })();