MediaWiki:TimeSinceLastEdit.js
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:按 Ctrl-F5。
window.RLQ = window.RLQ || [];
window.RLQ.push(function() {
$(function() {
mw.loader.using("moment").then(function() {
// real humanize
function humanize(then) {
var now = moment();
var isBefore = then.isBefore(now);
var monthsHave31Days = [0, 2, 4, 6, 7, 9, 11]; // 月份从0开始
var year = isBefore ? now.year() - then.year() : then.year() - now.year(),
month = isBefore ? now.month() - then.month() : then.month() - now.month(),
day = isBefore ? now.date() - then.date() : then.date() - now.date(),
hour = isBefore ? now.hour() - then.hour() : then.hour() - now.hour(),
minute = isBefore ? now.minute() - then.minute() : then.minute() - now.minute(),
second = isBefore ? now.second() - then.second() : then.second() - now.second();
if (second < 0) {
minute--;
second += 60;
}
if (minute < 0) {
hour--;
minute += 60;
}
if (hour < 0) {
day--;
hour += 24;
}
if (day < 0) {
month--;
if (monthsHave31Days.includes((isBefore ? then : now).month())) {
day += 31;
} else if ((isBefore ? then : now).month() === 1) {
if ((isBefore ? then : now).year() % 4 === 0) {
day += 29;
} else {
day += 28;
}
} else {
day += 30;
}
}
if (month < 0) {
year--;
month += 12;
}
var result = "";
if (year > 0) {
result += year + "年";
}
if (month > 0) {
result += month + "个月";
} else if (result !== "") {
result += "0个月";
}
if (day > 0) {
result += day + "天";
} else if (result !== "") {
result += "0天";
}
if (hour > 0) {
result += hour + "小时";
} else if (result !== "") {
result += "0小时";
}
if (minute > 0) {
result += minute + "分";
} else if (result !== "") {
result += "0分";
}
if (second > 0) {
result += second + "秒";
} else if (result !== "") {
result += "0秒";
}
return result + (isBefore ? "前" : "后");
}
/**
* Refresh
*/
function tsRefreshDuration() {
$('.lastEditTime').each(function() {
// Sanity check
if (!this.dataset || !this.dataset.lastEditTime) return;
// FIX BY CASE: In Moegirlpedia's deployment, the Epoch timestamp is incorrect (with 8 hours' difference)
// We will fix this by subtract 8 hours (28800 seconds)
var lastEditTime = moment.unix(this.dataset.lastEditTime - 28800);
if (!lastEditTime || !lastEditTime.isValid()) return;
// Diff and sanity check
var editTimeDiff = moment.duration(moment().diff(lastEditTime));
if (!editTimeDiff || !editTimeDiff.isValid()) {
// FIX BY CASE (@Yurin Jasmine): Remind user about local time issue
$(this).text("未知时长(请校准您的设备时间再试)");
return;
}
// Set time
$(this).text(humanize(lastEditTime) + (editTimeDiff.asMilliseconds() < 0 ? "(请校准您的设备时间再试)" : ""));
});
}
window.setInterval(function() { tsRefreshDuration(); }, 1000);
// First call
tsRefreshDuration();
});
});
});