Skip to content

Commit

Permalink
main.user.js: 修复时间元素on开头
Browse files Browse the repository at this point in the history
1. 固化时间元素正则规则
2. 恢复`时间元素监视`, 持续替换`on`开头时间
  • Loading branch information
maboloshi committed Oct 27, 2024
1 parent 2f8392b commit 4690eaf
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions main.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
switch (node.tagName) {
case "RELATIVE-TIME": // 翻译时间元素
transTimeElement(node.shadowRoot);
watchTimeElement(node.shadowRoot);
return;

case "INPUT":
Expand Down Expand Up @@ -249,17 +250,29 @@
*/
function transTimeElement(el) {
const text = el.childNodes.length > 0 ? el.lastChild.textContent : el.textContent;
const res = I18N[lang]['public']['time-regexp']; // 时间正则规则

for (let [a, b] of res) {
const translatedText = text.replace(a, b);
if (translatedText !== text) {
el.textContent = translatedText;
break;
}
const translatedText = text.replace(/^on/, "");
if (translatedText !== text) {
el.textContent = translatedText;
}
}

/**
* watchTimeElement 函数:监视时间元素变化, 触发和调用时间元素翻译
* @param {Element} el - 需要监视的元素。
*/
function watchTimeElement(el) {
const MutationObserver =
window.MutationObserver ||
window.WebKitMutationObserver ||
window.MozMutationObserver;

new MutationObserver(mutations => {
transTimeElement(mutations[0].addedNodes[0]);
}).observe(el, {
childList: true
});
}

/**
* transElement 函数:翻译指定元素的文本内容或属性。
* @param {Element|DOMStringMap} el - 需要翻译的元素或元素的数据集 (node.dataset)。
Expand Down

0 comments on commit 4690eaf

Please sign in to comment.