试试:
// ==UserScript==
// @name 知乎评论时间精确到秒
// @namespace http://tampermonkey.net/
// @version 0.3
// @description try to take over the world!
// @author You
// @match https://www.zhihu.com/*
// @match https://zhuanlan.zhihu.com/*
// @icon http://zhihu.com/favicon.ico
// @grant GM_addStyle
// @run-at document-start
// @require https://scriptcat.org/lib/637/1.3.3/ajaxHooker.js
// ==/UserScript==
(function () {
'use strict';
function timestampToTime(timestamp) {
const milliseconds = timestamp * 1000;
const date = new Date(milliseconds);
const year = date.getFullYear();
const month = addZero(date.getMonth() + 1);
const day = addZero(date.getDate());
const hour = addZero(date.getHours());
const minute = addZero(date.getMinutes());
const second = addZero(date.getSeconds());
return `${ year }-${ month }-${ day } ${ hour }:${ minute }:${ second }`;
}
function addZero(num) {
return num < 10 ? `0${ num }` : `${ num }`;
}
ajaxHooker.hook(request => {
if (request.url.includes("https://www.zhihu.com/api/v4/comment_v5/comment/") || request.url.includes("https://www.zhihu.com/api/v4/comment_v5/answers/") || request.url.includes("https://www.zhihu.com/api/v4/comment_v5/articles/")) {
request.response = res => {
// console.log('\n== ↓ ↓ ↓ ↓ ↓ == \n', res)
if (res.json.data) {
res.json.data.forEach(item => {
// console.log(timestampToTime(item.created_time));
item.content = item.content + '<span class="commentTime">' + timestampToTime(item.created_time) + '</span>'
if (item.child_comments.length >= 1) {
item.child_comments.forEach(child => {
child.content = child.content + '<span class="commentTime">' + timestampToTime(child.created_time) + '</<span>'
});
}
});
GM_addStyle(`
.CommentContent {
position: relative;
overflow: visible;
}
/* 精确时间 */
.commentTime {
position: absolute;
left: 0;
bottom: -25px;
color: #999;
}
.css-140jo2 {
position: relative;
}
/* 原时间 */
.css-12cl38p,
.css-12cl38p + span,
.css-nm6sok + span {
display: none;
}
/* ip属地 */
.css-8hxn0r .css-nm6sok {
position: absolute;
left: 160px;
top: 3px;
}
/* 热评 */
.css-8hxn0r .css-33kuns {
position: absolute;
left: 240px;
top: 3px;
}
/* 作者置顶 */
.css-1o87v1m{
position: absolute;
top: 22px;
left: -4px;
}
`);
}
};
}
});
})();