如何编写Userscript转换网页时区?Chrome Tampermonkey时区转换方案问询
Hey there! Let's break down your two questions step by step:
1. Existing Userscripts for Time Zone Conversion
- While there are some general-purpose time zone conversion userscripts out there, most are designed for global timestamp conversion or require manual triggers. It's rare to find one that specifically targets ET/EST to CET conversion only for table cells. You could search Tampermonkey's script library for keywords like "time zone converter table ET to CET", but you'll likely need to tweak any existing script to fit your exact needs. For a tailored solution, writing a custom script is the most reliable approach.
2. Custom Tampermonkey Script to Convert ET/EST to CET in Table Cells
I’ve put together a script that targets <table>/<td> elements, converts any ET/EST timestamps to CET, and handles dynamically loaded content (like tables loaded via AJAX). Here’s how it works:
The Script
// ==UserScript== // @name ET/EST to CET Time Converter (Table Cells) // @namespace http://tampermonkey.net/ // @version 0.1 // @description Convert ET/EST timestamps in table <td> elements to CET // @author Your Name // @match *://*/* // @grant none // ==/UserScript== (function() { 'use strict'; // Convert ET/EST time string to formatted CET function convertEtToCET(timeStr) { // Adjust regex if your target time format differs (e.g., "YYYY-MM-DD HH:mm ET") const etRegex = /(\d{1,2}\/\d{1,2}\/\d{4} \d{1,2}:\d{2}) (ET|EST)/i; const match = timeStr.match(etRegex); if (!match) return timeStr; // Return original text if no ET/EST timestamp found const [, etTime, tz] = match; // Handle ET (covers EST/EDT: EST=UTC-5, EDT=UTC-4; Date auto-adjusts for DST) const etDate = new Date(`${etTime} UTC-0${tz === 'EST' ? '5' : '4'}`); if (isNaN(etDate.getTime())) return timeStr; // Skip invalid dates // Format CET output (adjust options to match your preferred style) const cetOptions = { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', timeZone: 'Europe/Paris' // Use 'Europe/Berlin' for alternate CET region }; const cetTime = etDate.toLocaleString('en-GB', cetOptions).replace(',', '') + ' CET'; return timeStr.replace(etRegex, cetTime); } // Scan all table cells and convert timestamps function processTableCells() { const tds = document.querySelectorAll('table td'); tds.forEach(td => { td.textContent = convertEtToCET(td.textContent); }); } // Run initial conversion when page loads processTableCells(); // Watch for dynamic content (e.g., AJAX-loaded tables) const observer = new MutationObserver((mutations) => { mutations.forEach(mutation => { if (mutation.addedNodes.length) { mutation.addedNodes.forEach(node => { if (node.nodeType === Node.ELEMENT_NODE) { if (node.tagName === 'TABLE' || node.querySelector('table')) { processTableCells(); } } }); } }); }); observer.observe(document.body, { childList: true, subtree: true }); })();
Key Notes & Setup Tips
- Restrict to specific websites: Change the
@matchline to target only your desired site (e.g.,https://your-target-site.com/*instead of*://*/*). - Adjust time format: If your target timestamps use a different pattern (like "YYYY-MM-DD HH:mm ET"), modify the
etRegexpattern to match it. - CET region: Swap
Europe/PariswithEurope/Berlinor another CET timezone if needed—this ensures automatic handling of CEST (夏令时). - How to use:
- Open Tampermonkey and click "Create a new script".
- Replace the default code with the script above.
- Save the script, then refresh your target webpage.
内容的提问来源于stack exchange,提问作者Johndbblddoe
相关产品推荐
相关产品推荐

