MT Convert to Local Time

Override JavaScript's Date object to always use Beijing time (UTC+8), and convert all date and time values on a webpage from Beijing Time (GMT+8) to your local time zone.

目前為 2024-04-25 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/493489/1366399/MT%20Convert%20to%20Local%20Time.js

  1. // ==UserScript==
  2. // @name MT Convert to Local Time
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description Override JavaScript's Date object to always use Beijing time (UTC+8), and convert all date and time values on a webpage from Beijing Time (GMT+8) to your local time zone.
  6. // @author tttsc, GPT4
  7. // @match https://*.m-team.cc/*
  8. // @match https://*.m-team.io/*
  9. // @match https://test2.m-team.cc/*
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. const originalDate = Date;
  18.  
  19. // Function to adjust the local time to Beijing time
  20. function toBeijingTime(original) {
  21. const localTime = new originalDate(original);
  22. const localTimeMs = localTime.getTime();
  23. const localOffset = localTime.getTimezoneOffset() * 60000; // Offset in milliseconds
  24. const beijingOffset = 8 * 3600 * 1000; // Beijing is UTC+8
  25. return new originalDate(localTimeMs + localOffset + beijingOffset);
  26. }
  27.  
  28. // Override the Date object
  29. Date = function () {
  30. if (arguments.length === 0) {
  31. return toBeijingTime(originalDate.now());
  32. } else if (arguments.length === 1) {
  33. return new originalDate(arguments[0]);
  34. } else {
  35. return new originalDate(...arguments);
  36. }
  37. };
  38.  
  39. Date.prototype = originalDate.prototype;
  40. Date.now = function() {
  41. return toBeijingTime(originalDate.now()).getTime();
  42. };
  43. Date.parse = originalDate.parse;
  44. Date.UTC = originalDate.UTC;
  45. Object.defineProperty(Date, 'prototype', { writable: false });
  46. console.log('Tampermonkey script loaded: Starting conversion...');
  47.  
  48. // Function to format dates into YYYY-MM-DD HH:MM:SS format
  49. function formatDateTime(date) {
  50. const year = date.getFullYear();
  51. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  52. const day = date.getDate().toString().padStart(2, '0');
  53. const hours = date.getHours().toString().padStart(2, '0');
  54. const minutes = date.getMinutes().toString().padStart(2, '0');
  55. const seconds = date.getSeconds().toString().padStart(2, '0');
  56. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  57. }
  58.  
  59. // Function to convert time from Beijing to local
  60. function convertTimeToLocal(timeString) {
  61. const beijingTimeOffset = 8; // Beijing is UTC+8
  62. const dateInBeijing = new Date(timeString + ' UTC+0800'); // Parse the Beijing time
  63. return formatDateTime(dateInBeijing); // Convert to local time string in fixed format
  64. }
  65.  
  66. // Function to process and replace date strings within an element's text
  67. function processTextNode(node) {
  68. const timePattern = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}|\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2}/g; // Example patterns, modify as needed
  69. if (node.nodeValue.match(timePattern)) {
  70. node.nodeValue = node.nodeValue.replace(timePattern, match => convertTimeToLocal(match));
  71. console.log(`Processed text node: `, node.nodeValue);
  72. }
  73. }
  74.  
  75. // Function to process and replace date strings within an element's title attribute
  76. function processTitleAttribute(element) {
  77. const timePattern = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}|\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2}/g; // Example patterns, modify as needed
  78. if (element.title && element.title.match(timePattern)) {
  79. element.title = element.title.replace(timePattern, match => convertTimeToLocal(match));
  80. console.log(`Processed title attribute: `, element.title);
  81. }
  82. }
  83.  
  84. // Observer to handle dynamic content
  85. const observer = new MutationObserver(mutations => {
  86. mutations.forEach(mutation => {
  87. mutation.addedNodes.forEach(node => {
  88. if (node.nodeType === Node.TEXT_NODE) {
  89. processTextNode(node);
  90. } else if (node.nodeType === Node.ELEMENT_NODE) {
  91. processTitleAttribute(node);
  92. node.querySelectorAll('*').forEach(child => {
  93. processTitleAttribute(child);
  94. child.childNodes.forEach(childNode => {
  95. if (childNode.nodeType === Node.TEXT_NODE) {
  96. processTextNode(childNode);
  97. }
  98. });
  99. });
  100. }
  101. });
  102. });
  103. });
  104.  
  105. // Start observing
  106. observer.observe(document.body, {
  107. childList: true,
  108. subtree: true
  109. });
  110.  
  111. // Initial processing on load
  112. document.querySelectorAll('time, span, div, p, a, *[title]').forEach(element => {
  113. processTitleAttribute(element);
  114. element.childNodes.forEach(node => {
  115. if (node.nodeType === Node.TEXT_NODE) {
  116. processTextNode(node);
  117. }
  118. });
  119. });
  120. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址