此脚本已停止使用 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.

Ovu skriptu ne treba izravno instalirati. To je biblioteka za druge skripte koje se uključuju u meta direktivu // @require https://update.greasyfork.org/scripts/493489/1400090/%E6%AD%A4%E8%84%9A%E6%9C%AC%E5%B7%B2%E5%81%9C%E6%AD%A2%E4%BD%BF%E7%94%A8%20MT%20Convert%20to%20Local%20Time.js

  1. // ==UserScript==
  2. // @name MT Convert to Local Time
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3.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. // @downloadURL https://update.greasyfork.org/scripts/493489/MT%20Convert%20to%20Local%20Time.user.js
  13. // @updateURL https://update.greasyfork.org/scripts/493489/MT%20Convert%20to%20Local%20Time.meta.js
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18.  
  19. const originalDate = Date;
  20.  
  21. // Function to adjust the local time to Beijing time
  22. function toBeijingTime(original) {
  23. const localTime = new originalDate(original);
  24. const localTimeMs = localTime.getTime();
  25. const localOffset = localTime.getTimezoneOffset() * 60000; // Offset in milliseconds
  26. const beijingOffset = 8 * 3600 * 1000; // Beijing is UTC+8
  27. return new originalDate(localTimeMs + localOffset + beijingOffset);
  28. }
  29.  
  30. // Override the Date object
  31. Date = function () {
  32. if (arguments.length === 0) {
  33. return toBeijingTime(originalDate.now());
  34. } else if (arguments.length === 1) {
  35. return new originalDate(arguments[0]);
  36. } else {
  37. return new originalDate(...arguments);
  38. }
  39. };
  40.  
  41. Date.prototype = originalDate.prototype;
  42. Date.now = function() {
  43. return toBeijingTime(originalDate.now()).getTime();
  44. };
  45. Date.parse = originalDate.parse;
  46. Date.UTC = originalDate.UTC;
  47. Object.defineProperty(Date, 'prototype', { writable: false });
  48. console.log('Tampermonkey script loaded: Starting conversion...');
  49.  
  50. // Function to format dates into YYYY-MM-DD HH:MM:SS format
  51. function formatDateTime(date) {
  52. const year = date.getFullYear();
  53. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  54. const day = date.getDate().toString().padStart(2, '0');
  55. const hours = date.getHours().toString().padStart(2, '0');
  56. const minutes = date.getMinutes().toString().padStart(2, '0');
  57. const seconds = date.getSeconds().toString().padStart(2, '0');
  58. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  59. }
  60.  
  61. // Function to convert time from Beijing to local
  62. function convertTimeToLocal(timeString) {
  63. // Correct format for ISO 8601 with timezone
  64. const formattedTimeString = timeString.replace(' ', 'T') + '+08:00';
  65. const dateInBeijing = new Date(formattedTimeString); // Parse the Beijing time
  66. return formatDateTime(dateInBeijing); // Convert to local time string in fixed format
  67. }
  68.  
  69. // Function to process and replace date strings within an element's text
  70. function processTextNode(node) {
  71. 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
  72. if (node.nodeValue.match(timePattern)) {
  73. node.nodeValue = node.nodeValue.replace(timePattern, match => convertTimeToLocal(match));
  74. console.log(`Processed text node: `, node.nodeValue);
  75. }
  76. }
  77.  
  78. // Function to process and replace date strings within an element's title attribute
  79. function processTitleAttribute(element) {
  80. 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
  81. if (element.title && element.title.match(timePattern)) {
  82. element.title = element.title.replace(timePattern, match => convertTimeToLocal(match));
  83. console.log(`Processed title attribute: `, element.title);
  84. }
  85. }
  86.  
  87. // Observer to handle dynamic content
  88. const observer = new MutationObserver(mutations => {
  89. mutations.forEach(mutation => {
  90. mutation.addedNodes.forEach(node => {
  91. if (node.nodeType === Node.TEXT_NODE) {
  92. processTextNode(node);
  93. } else if (node.nodeType === Node.ELEMENT_NODE) {
  94. processTitleAttribute(node);
  95. node.querySelectorAll('*').forEach(child => {
  96. processTitleAttribute(child);
  97. child.childNodes.forEach(childNode => {
  98. if (childNode.nodeType === Node.TEXT_NODE) {
  99. processTextNode(childNode);
  100. }
  101. });
  102. });
  103. }
  104. });
  105. });
  106. });
  107.  
  108. // Start observing
  109. observer.observe(document.body, {
  110. childList: true,
  111. subtree: true
  112. });
  113.  
  114. // Initial processing on load
  115. document.querySelectorAll('time, span, div, p, a, *[title]').forEach(element => {
  116. processTitleAttribute(element);
  117. element.childNodes.forEach(node => {
  118. if (node.nodeType === Node.TEXT_NODE) {
  119. processTextNode(node);
  120. }
  121. });
  122. });
  123.  
  124. // Traverse DOM elements to apply text node processing
  125. function traverseAndProcess(element) {
  126. element.childNodes.forEach(child => {
  127. if (child.nodeType === Node.TEXT_NODE) {
  128. processTextNode(child);
  129. } else if (child.nodeType === Node.ELEMENT_NODE) {
  130. traverseAndProcess(child);
  131. }
  132. });
  133. }
  134. window.addEventListener('load', () => {
  135. setTimeout(() => {
  136. document.querySelectorAll('.ant-typography-secondary').forEach(traverseAndProcess);
  137. console.log("Script processed elements after window load");
  138. }, 1000); // Adjust delay as necessary
  139. });
  140.  
  141. })();