Skip to content

Commit

Permalink
Revert "完善功能:删除多余watchTimeElement 函数,调整时间元素翻译和时间元素正则" (c1a2ffe)
Browse files Browse the repository at this point in the history
  • Loading branch information
maboloshi committed Jun 8, 2024
1 parent 8d2613f commit d50798b
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 3 deletions.
117 changes: 116 additions & 1 deletion locals.js
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,122 @@ I18N.zh["pubilc"] = { // 公共区域翻译
}],
],
"time-regexp": [ // 时间正则翻译专项
[/on/, ""],
/**
* 匹配时间格式
*
* 月 日 或 月 日, 年
* Mar 19, 2015 – Mar 19, 2016
* January 26 – March 19
* March 26
*
* 不知道是否稳定, 暂时先试用着. 2016-03-19 20:46:45
*
* 更新于 2021-10-04 15:19:18
* 增加 带介词 on 的格式,on 翻译不体现
* on Mar 19, 2015
* on March 26
*
* 更新于 2021-10-10 13:44:36
* on 星期(简写), 月 日 年 // 个人访问令牌 有效期
* on Tue, Nov 9 2021
*
* 2021-10-19 12:04:19 融合更多规则
*
* 4 Sep
* 30 Dec 2020
*
* on 4 Sep
* on 30 Dec 2020
*
* 2021-11-22 12:51:57 新增 格式
*
* 星期(全称), 月 日, 年 // 仓库-->洞察-->流量 图示标识
* Sunday, November 14, 2021
*
* Tip:
* 正则中的 ?? 前面的字符 重复0次或1次
* 正则中的 ?: 非捕获符号(即关闭圆括号的捕获能力) 使用方法 (?: 匹配规则) -->该匹配不会被捕获 为 $数字
*/
[/(?:on |)(?:(\d{1,2}) |)(?:(Sun(?:day)?|Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?), |)(?:(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May(?:)??|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)(?:,? |$))(\d{4}|)(\d{1,2}|)(?:,? (\d{4})|)/g, function (all, date1, week, month, year1, date2, year2) {
var weekKey = {
"Sun" : "周日",
"Mon" : "周一",
"Tue" : "周二",
"Wed" : "周三",
"Thu" : "周四",
"Fri" : "周五",
"Sat" : "周六",
};
var monthKey = {
"Jan": "1月",
"Feb": "2月",
"Mar": "3月",
"Apr": "4月",
"May": "5月",
"Jun": "6月",
"Jul": "7月",
"Aug": "8月",
"Sep": "9月",
"Oct": "10月",
"Nov": "11月",
"Dec": "12月"
};
var date = date1 ? date1 : date2;
var year = year1 ? year1 : year2;
return (year ? year + '年' : '') + monthKey[month.substring(0, 3)] + (date ? date + '日' : '') + (week ? ', ' + weekKey[week.substring(0, 3)] : '');
}],
/**
* 相对时间格式处理
*
* 更新于 2021-11-21 16:47:14
* 1. 添加 前缀词
* over xxx ago // 里程碑页面 最后更新时间
* about xxx ago // 里程碑页面 最后更新时间
* almost xxx ago // 里程碑页面 最后更新时间
* less than xxx ago // 导出帐户数据
* 2. xxx之内的相对时间格式
* in 6 minutes // 拉取请求页面
*
* 更新于 2021-11-22 11:54:30
* 1. 修复 Bug: 意外的扩大了匹配范围(不带前缀与后缀的时间) 干扰了带有相对时间的其他规则
* 7 months
*/
[/^just now|^now|^last year|^last month|^last week|^yesterday|(?:(over|about|almost|in) |)(an?|\d+)(?: |)(second|minute|hour|day|month|year|week)s?( ago|)/, function (all, prefix, count, unit, suffix) {
if (all === 'now') {
return '现在';
}
if (all === 'just now') {
return '刚刚';
}
if (all === 'last year') {
return '最近 1 年';
}
if (all === 'last month') {
return '上个月';
}
if (all === 'last week') {
return '上周';
}
if (all === 'yesterday') {
return '昨天';
}
if (count[0] === 'a') {
count = '1';
} // a, an 修改为 1

var unitKey = {second: '秒', minute: '分钟', hour: '小时', day: '天', month: '个月', year: '年', week: '周'};

if (suffix) {
return (prefix === 'about' || prefix === 'almost' ? '大约 ' : prefix === 'less than' ? '不到 ' : '') + count + ' ' + unitKey[unit] + (prefix === 'over' ? '多之前' : '之前');
} else {
return count + ' ' + unitKey[unit] + (prefix === 'in' ? '之内' : '之前');
}
}],
[/(\d+)(h|d|w|m)/, function (all, count, suffix) {
var suffixKey = {h: '小时', d: '天', w: '周', m: '个月'};

return count + ' ' + suffixKey[suffix] + '之前';
}],
],
};

Expand Down
27 changes: 25 additions & 2 deletions main.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,15 @@
if (node.nodeType === Node.ELEMENT_NODE) { // 元素节点处理

// 翻译时间元素
if (node.tagName === 'RELATIVE-TIME') {
transTimeElement(node.shadowRoot);
if (
["RELATIVE-TIME", "TIME-AGO", "TIME", "LOCAL-TIME"].includes(node.tagName)
) {
if (node.shadowRoot) {
transTimeElement(node.shadowRoot);
watchTimeElement(node.shadowRoot);
} else {
transTimeElement(node);
}
return;
}

Expand Down Expand Up @@ -256,6 +263,22 @@
}
}

/**
* 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 函数:翻译指定元素的文本内容或属性。
Expand Down

0 comments on commit d50798b

Please sign in to comment.