|
// ==UserScript== |
|
// @name [XDA] Site Helper |
|
// @description 回文標記幾天前 |
|
// @author NKid |
|
// @version 2013-08-09 |
|
// @include http://forum.xda-developers.com/showthread.php?* |
|
// @include http://forum.xda-developers.com/showpost.php?p=* |
|
// ==/UserScript== |
|
Date.prototype.toLongDateTimeString = function() { |
|
return (this.getFullYear() + '/' + (this.getMonth() + 1) + '/' + this.getDate() + ' ' + padLeft(this.getHours(), 2) + ':' + padLeft(this.getMinutes(), 2)); |
|
} |
|
|
|
function padLeft(nr, n, str) { |
|
return Array(n - String(nr).length + 1).join(str || '0') + nr; |
|
} |
|
|
|
function elapsedDays(dtStartDay) { |
|
var dtNow = new Date(); |
|
var dtToday = new Date(); |
|
var intTotalDay, intY, intM, intD, strTime = ''; |
|
dtToday.setFullYear(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate()); |
|
intTotalDay = parseInt((dtToday - dtStartDay) / 86400000, 10); |
|
intY = Math.floor(intTotalDay / 365); |
|
intM = Math.floor((intTotalDay - intY * 365) / 30); |
|
intD = (intTotalDay - intY * 365 - intM * 30); |
|
if (intY > 0) strTime = intY + '年 '; |
|
if (intM > 0) strTime += intM + '個月 '; |
|
if (intD > 0) strTime += intD + '天'; |
|
return strTime; |
|
} |
|
|
|
function chkStringDayName(n) { |
|
return !/Today|Yesterday/.test(n); |
|
} |
|
|
|
function convertPostDate(eles) { |
|
for (var i = 0; i < eles.length; i++) { |
|
if (chkStringDayName(eles[i].textContent)) { |
|
var strPostDate = eles[i].textContent.replace(/th|st|nd|rd|,/g, ''); |
|
var dtPostDate = new Date(strPostDate); |
|
var intElapsedDays = elapsedDays(dtPostDate) |
|
var strTmp = dtPostDate.toLongDateTimeString() + ' (' + intElapsedDays + ')'; |
|
eles[i].innerHTML = strTmp; |
|
} |
|
} |
|
} |
|
|
|
function convertLastEdit(eles) { |
|
for (var i = 0; i < eles.length; i++) { |
|
var strLastEdit = /;\s(.*)<span\sclass=\"time\">(.*)<\/span>\./.exec(eles[i].innerHTML); |
|
var dtLastEdit = new Date(strLastEdit[1].replace(/th|st|nd|rd|at/g, '') + ' ' + strLastEdit[2]); |
|
if (chkStringDayName(strLastEdit)) { |
|
var intElapsedDays = elapsedDays(dtLastEdit) |
|
var strTmp = ' ' + dtLastEdit.toLongDateTimeString() + ' (' + intElapsedDays + ')'; |
|
eles[i].innerHTML = eles[i].innerHTML.replace(strLastEdit[0], strTmp); |
|
} |
|
} |
|
} |
|
|
|
convertPostDate(document.querySelectorAll('.postDate')); |
|
convertLastEdit(document.querySelectorAll('.lastEdited')); |
|
window.addEventListener("AutoPagerize_DOMNodeInserted", function(e) { |
|
convertPostDate(e.target.querySelectorAll('.postDate')); |
|
convertLastEdit(e.target.querySelectorAll('.lastEdited')); |
|
}, false); |