Created
June 14, 2024 01:48
-
-
Save hyrious/cc73c0ed9dd7c01ca688306fdae44824 to your computer and use it in GitHub Desktop.
Translate English date string to Chinese representation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Translate Date | |
// @name:zh-CN 翻译日期到中文格式 | |
// @namespace translate-date.hyrious.me | |
// @match *://*/* | |
// @exclude *://*.bilibili.com/video/* | |
// @grant none | |
// @version 1.0 | |
// @author hyrious | |
// @description May 25 -> 3月25日 | |
// ==/UserScript== | |
(async function(){ | |
await new Promise(function (resolve) { setTimeout(resolve, 1000) }) | |
var d = new Date(), short = [], long = []; | |
for (var i = 0; i < 12; i++) { | |
d.setMonth(i) | |
short.push(d.toLocaleDateString('en-US', { month: 'short' })) | |
long.push(d.toLocaleDateString('en-US', { month: 'long' })) | |
} | |
var testRE = new RegExp('\\b(' + short.join('|') + '|' + long.join('|') + ') \\d') | |
var w = document.createTreeWalker( | |
document.body, | |
NodeFilter.SHOW_TEXT, | |
function filter(node) { | |
if (testRE.test(node.data)) return NodeFilter.FILTER_ACCEPT | |
} | |
) | |
var replaceRE = new RegExp('\\b(' + short.join('|') + '|' + long.join('|') + ') (\\d+)', 'g') | |
while (w.nextNode()) { | |
var node = w.currentNode | |
node.data = node.data.replace(replaceRE, function (_, month, date) { | |
var i = short.indexOf(month) | |
if (i == -1) i = long.indexOf(month) | |
if (i >= 0) { | |
return (i + 1) + '月' + date + '日' | |
} | |
return _ | |
}) | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment