Last active
January 26, 2016 06:43
-
-
Save dofy/38c8d67405a5597f4e7e to your computer and use it in GitHub Desktop.
显示 v2ex 绝对时间
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name v2ex 绝对时间 | |
// @namespace http://phpz.org/ | |
// @version 0.1.5 | |
// @description 显示 v2ex 中的绝对时间 | |
// @author Seven Yu | |
// @match *.v2ex.com/* | |
// @run-at document-end | |
// @grant none | |
// @updateURL https://gist.github.com/dofy/38c8d67405a5597f4e7e/raw | |
// @downloadURL https://gist.github.com/dofy/38c8d67405a5597f4e7e/raw | |
// ==/UserScript== | |
/* jshint -W097 */ | |
'use strict'; | |
(function(win, doc, $) { | |
// 检测列表 | |
var classList = [ | |
'.header .gray', // 楼主时间 | |
'.cell .small', // 列表时间 | |
'.inner .small', // 最后一条 | |
'.dock_area .fade', // 个人主页回复列表 | |
'.subtle .fade', // 附言栏 | |
'.status_time', // timeline | |
'.cell .snow' // 系统提醒 | |
]; | |
// 时间正则 | |
/* | |
* RegExp.exec() 返回结构说明 | |
* 0: 匹配的时间文本 | |
* 1: 天数 | |
* 2: 小时数 | |
* 3: 分钟数 | |
*/ | |
var dateReg = /(?:(\d+)\s*天前)|(?:(?:(\d+)\s*小时\s*)?(\d+)\s*分钟前)|(?:几秒前|刚刚)/g; | |
// 执行检查和替换 | |
for(var ind in classList) { | |
$(classList[ind]).each(function(index, item) { | |
var showDate, itemContent, matchArray; | |
item = $(item); | |
dateReg.lastIndex = 0; | |
itemContent = item.html(); | |
matchArray = dateReg.exec(itemContent); | |
showDate = realDate(matchArray); | |
if(showDate) { | |
item.html(itemContent.replace(matchArray[0], showDate)); | |
} | |
}); | |
} | |
/** | |
* 获取绝对时间 | |
*/ | |
function realDate(matchArray) { | |
if(!matchArray) | |
return false; | |
var now = new Date(), | |
nowHour = now.getHours(), | |
dayNum = matchArray[1] || 0, | |
hourNum = matchArray[2] || 0, | |
minuteNum = matchArray[3] || 0, | |
itemNow, resultDate, resultTime; | |
now.setDate(now.getDate() - dayNum); | |
now.setHours(now.getHours() - hourNum); | |
now.setMinutes(now.getMinutes() - minuteNum); | |
itemNow = new Date(now); | |
resultDate = [itemNow.getFullYear(), itemNow.getMonth() + 1, itemNow.getDate()].join('-'); | |
resultTime = [fixZero(itemNow.getHours()), fixZero(itemNow.getMinutes())].join(':'); | |
if(dayNum > 0) { | |
return resultDate; | |
} else { | |
if(hourNum >= nowHour) { | |
return [resultDate, resultTime].join(' '); | |
} else { | |
return resultTime; | |
} | |
} | |
} | |
/** | |
* 数字补零 | |
*/ | |
function fixZero(num) { | |
return num > 9 ? num : '0' + num; | |
} | |
})(window, document, window.jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment