Fake Date with Controls

Set a fake date in your browser globally using Tampermonkey with UI controls

  1. // ==UserScript==
  2. // @name Fake Date with Controls
  3. // @namespace fake-date-script
  4. // @version 1.3
  5. // @description Set a fake date in your browser globally using Tampermonkey with UI controls
  6. // @match http://*/*
  7. // @match https://*/*
  8. // @grant none
  9. // @license MIT
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Check if we should use real time
  17. const useRealTime = localStorage.getItem('fakeDate_useRealTime') === 'true';
  18.  
  19. // Use a stored date or the default one
  20. const storedDate = localStorage.getItem('fakeDateValue') || '2025-04-01T00:00:00.000Z';
  21. let fakeDateValue = storedDate;
  22. let fakeDate = new Date(fakeDateValue);
  23. let fakeTimestamp = fakeDate.getTime();
  24. let baseTime = Date.now(); // Capture the real time when script loads
  25.  
  26. // If useRealTime is true, set timeDifference to 0
  27. let timeDifference = useRealTime ? 0 : (fakeTimestamp - baseTime);
  28. const OriginalDate = Date;
  29.  
  30. function FakeDate(...args) {
  31. if (args.length === 0) {
  32. // Return current fake date when no args provided
  33. return new OriginalDate(OriginalDate.now() + timeDifference);
  34. }
  35. return new OriginalDate(...args);
  36. }
  37.  
  38. // Ensure all static methods are properly copied
  39. FakeDate.now = function() {
  40. return OriginalDate.now() + timeDifference;
  41. };
  42.  
  43. FakeDate.parse = OriginalDate.parse;
  44. FakeDate.UTC = OriginalDate.UTC;
  45.  
  46. // Properly copy prototype chain
  47. FakeDate.prototype = OriginalDate.prototype;
  48. FakeDate.toString = function() { return OriginalDate.toString(); };
  49. FakeDate[Symbol.species] = OriginalDate;
  50.  
  51. // Override Date
  52. window.Date = FakeDate;
  53.  
  54. // Override performance.now() to align with fake date
  55. if (window.performance && window.performance.now) {
  56. const originalNow = window.performance.now.bind(window.performance);
  57. const perfTimeOrigin = performance.timeOrigin || 0;
  58.  
  59. window.performance.now = function() {
  60. return originalNow() + (timeDifference);
  61. };
  62.  
  63. // Update timeOrigin if it exists
  64. if ('timeOrigin' in window.performance) {
  65. Object.defineProperty(window.performance, 'timeOrigin', {
  66. get: function() {
  67. return perfTimeOrigin + timeDifference;
  68. }
  69. });
  70. }
  71. }
  72.  
  73. // Add UI functions
  74. function updateTimeDisplay() {
  75. const displayElem = document.getElementById('fake-time-display');
  76. if (displayElem) {
  77. displayElem.textContent = `Current Time: ${new Date().toLocaleString()}`;
  78. }
  79.  
  80. // Update status indicator
  81. const statusElem = document.getElementById('time-status');
  82. if (statusElem) {
  83. if (useRealTime) {
  84. statusElem.textContent = '(REAL TIME)';
  85. statusElem.style.color = '#4CAF50';
  86. } else {
  87. statusElem.textContent = '(FAKE TIME)';
  88. statusElem.style.color = '#f44336';
  89. }
  90. }
  91. }
  92.  
  93. // Create control panel when DOM is ready
  94. function createControlPanel() {
  95. // Add styles
  96. const styles = `
  97. #fake-date-control {
  98. position: fixed;
  99. bottom: 10px;
  100. right: 10px;
  101. background: white;
  102. border: 1px solid #ccc;
  103. border-radius: 5px;
  104. padding: 10px;
  105. z-index: 9999;
  106. box-shadow: 0 0 10px rgba(0,0,0,0.2);
  107. font-family: Arial, sans-serif;
  108. width: 280px;
  109. }
  110. #fake-date-control button {
  111. margin-right: 5px;
  112. margin-top: 5px;
  113. padding: 5px 10px;
  114. border-radius: 3px;
  115. border: 1px solid #ccc;
  116. background: #f0f0f0;
  117. cursor: pointer;
  118. }
  119. #fake-date-control button:hover {
  120. background: #e0e0e0;
  121. }
  122. #fake-time-display {
  123. margin-bottom: 5px;
  124. font-weight: bold;
  125. }
  126. #time-status {
  127. margin-bottom: 10px;
  128. font-weight: bold;
  129. }
  130. #refresh-page {
  131. background-color: #4CAF50 !important;
  132. color: white;
  133. }
  134. #reset-date {
  135. background-color: #f44336 !important;
  136. color: white;
  137. }
  138. `;
  139.  
  140. const styleElement = document.createElement('style');
  141. styleElement.textContent = styles;
  142. document.head.appendChild(styleElement);
  143.  
  144. // Create panel
  145. const panel = document.createElement('div');
  146. panel.id = 'fake-date-control';
  147. panel.innerHTML = `
  148. <div id="fake-time-display">Current Time: ${new Date().toLocaleString()}</div>
  149. <div id="time-status">${useRealTime ? '(REAL TIME)' : '(FAKE TIME)'}</div>
  150. <input type="datetime-local" id="fake-date-input">
  151. <div>
  152. <button id="set-date">Set Date</button>
  153. <button id="reset-date">Reset to Real Time</button>
  154. </div>
  155. <div>
  156. <button id="refresh-page">Refresh Page</button>
  157. <button id="hide-panel">Hide Panel</button>
  158. </div>
  159. `;
  160. document.body.appendChild(panel);
  161.  
  162. // Set initial input value
  163. const now = new Date();
  164. const year = now.getFullYear();
  165. const month = String(now.getMonth() + 1).padStart(2, '0');
  166. const day = String(now.getDate()).padStart(2, '0');
  167. const hours = String(now.getHours()).padStart(2, '0');
  168. const minutes = String(now.getMinutes()).padStart(2, '0');
  169.  
  170. document.getElementById('fake-date-input').value = `${year}-${month}-${day}T${hours}:${minutes}`;
  171.  
  172. // Add event listeners
  173. document.getElementById('set-date').addEventListener('click', function() {
  174. const dateInput = document.getElementById('fake-date-input').value;
  175. if (dateInput) {
  176. localStorage.setItem('fakeDateValue', new Date(dateInput).toISOString());
  177. localStorage.setItem('fakeDate_useRealTime', 'false');
  178. alert('Date set! Click Refresh to apply changes.');
  179. }
  180. });
  181.  
  182. document.getElementById('reset-date').addEventListener('click', function() {
  183. localStorage.setItem('fakeDate_useRealTime', 'true');
  184. alert('Reset to real time! Click Refresh to apply changes.');
  185. });
  186.  
  187. document.getElementById('refresh-page').addEventListener('click', function() {
  188. window.location.reload();
  189. });
  190.  
  191. document.getElementById('hide-panel').addEventListener('click', function() {
  192. panel.style.display = 'none';
  193.  
  194. // Create show button
  195. const showButton = document.createElement('button');
  196. showButton.textContent = useRealTime ? '🕒' : '🕒*';
  197. showButton.style.position = 'fixed';
  198. showButton.style.bottom = '10px';
  199. showButton.style.right = '10px';
  200. showButton.style.zIndex = '9999';
  201. showButton.style.padding = '5px 10px';
  202. showButton.style.borderRadius = '3px';
  203. showButton.style.border = '1px solid #ccc';
  204. showButton.style.background = useRealTime ? '#f0f0f0' : '#ffecec';
  205. showButton.style.cursor = 'pointer';
  206. showButton.addEventListener('click', function() {
  207. panel.style.display = 'block';
  208. showButton.remove();
  209. });
  210. document.body.appendChild(showButton);
  211. });
  212.  
  213. // Update time display every second
  214. setInterval(updateTimeDisplay, 1000);
  215. }
  216.  
  217. // Create control panel once the DOM is loaded
  218. if (document.readyState === 'loading') {
  219. document.addEventListener('DOMContentLoaded', createControlPanel);
  220. } else {
  221. setTimeout(createControlPanel, 500);
  222. }
  223.  
  224. console.log('Fake Date script loaded. Current fake date:', new Date().toISOString(), 'Using real time:', useRealTime);
  225. })();

QingJ © 2025

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