Skip to content

Instantly share code, notes, and snippets.

@hjdarnel
Last active February 25, 2025 00:35
Show Gist options
  • Save hjdarnel/4cf96197a53f18d64a69c7db058d6a72 to your computer and use it in GitHub Desktop.
Save hjdarnel/4cf96197a53f18d64a69c7db058d6a72 to your computer and use it in GitHub Desktop.
Convert MAM time to local time
// ==UserScript==
// @name MAM Convert to Local Time
// @namespace hjdarnel
// @version 0.0.1
// @description Change displayed time and date to your timezone without seconds
// @author hjdarnel
// @match *://*.myanonamouse.net/*
// @grant none
// @icon https://www.myanonamouse.net/favicon.ico
// @downloadURL https://gist.githubusercontent.com/hjdarnel/4cf96197a53f18d64a69c7db058d6a72/raw/convert.user.js
// @updateURL https://gist.githubusercontent.com/hjdarnel/4cf96197a53f18d64a69c7db058d6a72/raw/convert.user.js
// ==/UserScript==
"use strict";
// Format date without seconds
function formatDateWithoutSeconds(date) {
return date.toLocaleString([], {
year: "numeric",
month: "short",
day: "numeric",
hour: "numeric",
minute: "2-digit",
});
}
function updateTime(element) {
let originalDateStr = element.getAttribute("data-original-date");
if (!originalDateStr) {
originalDateStr = element.innerText;
element.setAttribute("data-original-date", originalDateStr);
}
// Parse date and adjust to show current time
const date = new Date(originalDateStr + " UTC");
const currentTime = new Date();
const secondsDiff = Math.floor(
(currentTime - new Date(originalDateStr + " UTC")) / 1000
);
date.setSeconds(date.getSeconds() + secondsDiff);
const formattedDate = formatDateWithoutSeconds(date);
// Only update DOM if the text has changed
if (element.innerText !== formattedDate) {
element.innerText = formattedDate;
}
}
// Set up observers and periodic updates
function initialize() {
const dateElements = document.querySelectorAll("#preNav > div");
dateElements.forEach((element) => {
updateTime(element);
// Observe for changes
const observer = new MutationObserver(() => updateTime(element));
observer.observe(element, { childList: true });
// Periodically update to keep time current
setInterval(() => updateTime(element), 10000);
});
}
// Run when DOM is ready
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initialize);
} else {
initialize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment