User:Mxn/CommentsInLocalTime.js
Appearance
< User:Mxn
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Mxn/CommentsInLocalTime. |
/**
* Tin nhắn theo giờ địa phương
* [[:vi:Wikipedia:Tin nhắn theo giờ địa phương]]
*
* Sửa lại các dấu thời gian trong chữ ký trong các trang thảo luận để sử dụng
* giờ địa phương tương đối dễ hiểu hơn giờ UTC tuyệt đối.
*
* Phỏng theo [[Wikipedia:Comments in Local Time]].
*/
$(function () {
if ([-1, 0, 8, 104, 106].indexOf(mw.config.get("wgNamespaceNumber")) !== -1 ||
["view", "submit"].indexOf(mw.config.get("wgAction")) === -1 ||
mw.config.get("wgCanonicalSpecialPageName") ||
mw.util.getParamValue("disable") === "loco") {
return;
}
//* Các tháng viết tắt trong tiếng Anh.
var enMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
/**
* Biểu thức chính quy khớp các dấu thời gian tại wiki này.
*
* Cho đến 2004:
* 18:16, 23 Dec 2004 (UTC)
* 2004–2005:
* 18:40, 26 tháng 12 2004 (UTC)
* 2005–nay:
* 01:30, ngày 10 tháng 7 năm 2013 (UTC)
*/
var dateRe = /(\d\d):(\d\d), (\d\d?) (Jan|January|Feb|February|Mar|March|Apr|April|May|June?|July?|Aug|August|Sept|September|Oct|October|Nov|November|Dec|December) (\d{4}) \(UTC\)/;
//* Các tên thẻ có thể chứa dấu thời gian trực tiếp.
var proseTags = ["DD", "LI", "P"];
//* Các tên thẻ không thể chứa dấu thời gian hoặc trực tiếp hoặc gián tiếp.
var codeTags = "code, input, pre, textarea, time";
// Tìm các nút văn bản DOM trong nội dung có thể chứa dấu thời gian. Phần
// mềm đã địa phương hóa những phần bên ngoài.
var root = $("#wikiPreview, #mw-content-text")[0];
if (!root) return;
var iter = document.createNodeIterator(root, NodeFilter.SHOW_TEXT, {
acceptNode: function (node) {
var isInProse = proseTags.indexOf(node.parentElement.nodeName) !== -1 ||
!$(node).parents(codeTags).length;
var isDateNode = isInProse && dateRe.test(node.data);
return isDateNode ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
},
});
// Cấu trúc lại các dấu thời gian đã kiếm được.
var prefixNode;
while ((prefixNode = iter.nextNode())) {
var result = dateRe.exec(prefixNode.data);
if (!result) continue;
var month = result[4] ? result[4] - 1 : enMonths.indexOf(result[5]);
var date = new Date(Date.UTC(result[6], month, result[3], result[1], result[2]));
// Tách dấu thời gian ra thành nút văn bản riêng.
var dateNode = prefixNode.splitText(result.index);
var suffixNode = dateNode.splitText(result[0].length);
// Bọc dấu thời gian vào trong phần tử thời gian.
var timeElt = $("<time />");
timeElt.addClass("localcomments explain");
timeElt.attr("datetime", date.toISOString());
$(dateNode).wrap(timeElt);
}
// Định dạng lại các dấu thời gian.
mw.loader.using("moment", function () {
$(".localcomments").each(function (idx, elt) {
var iso = $(elt).attr("datetime");
var theMoment = moment(iso);
// Định dạng các ngày gần đây. Hàm calendar() sử dụng định dạng
// “DD/MM/YYYY” không rõ cho những ngày cách đây lâu; sử dụng định
// dạng đầy đủ hơn trong trường hợp đó.
var cal = theMoment.calendar();
var diff = theMoment.diff(moment().startOf("day"), "days", true);
if (diff > -7 && diff < 8) $(elt).text(cal);
else $(elt).text(theMoment.format("LLL"));
// Thêm mẹo vặt có vài định dạng.
elt.title = theMoment.fromNow() + "\n" +
theMoment.format("LLLL[\n]YYYY-MM-DDTHH:mmZ");
});
});
});