Jump to content

User:Mxn/CommentsInLocalTime.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mxn (talk | contribs) at 14:44, 1 January 2016 (Created page with '/** * 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ý...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * 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");
		});
	});
});