Greasy Fork

Greasy Fork is available in English.

记录上次查看时间

记录上次查看时间。

当前为 2021-01-21 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         记录上次查看时间
// @version      0.1.0
// @include      https://www.mcbbs.net/*
// @author       xmdhs
// @description 记录上次查看时间。
// @namespace https://xmdhs.top
// ==/UserScript==

(function () {
    let tid;
    try {
        tid = document.querySelector("#pt > div > a:nth-child(9)").href.match(/thread-([0-9]{1,10})-1-1.html/)[1]
    } catch { }
    if (tid > 0) {
        let date = localStorage.getItem("lastview")
        if (date == null) {
            date = {}
        } else {
            date = JSON.parse(date)
        }
        date[tid] = new Date().getTime()
        let tempdate = {}
        if (date.length > 1000){
            let a = 0;
            for (let i in date) {
                a = a + 1
                if (a < 3) {
                    continue;
                }
                tempdate[i] = date[i]
            }
            date = tempdate
        }
        localStorage.setItem("lastview", JSON.stringify(date))
    } else {
        let date = localStorage.getItem("lastview")
        date = JSON.parse(date)
        jq(".s.xst").each(function () {
            let link = jq(this).attr("href")
            let tid = link.match(/thread-([0-9]{1,10})-1-[0-9]{1,10}.html/)
            let atime
            if (tid != null && tid.length >= 2){
                tid = tid[1]
                atime = date[tid]
            }else{
                let b = new URLSearchParams(link)
                let tidtemp = b.get("tid")            
                if (tidtemp != ""){
                    atime = date[tidtemp]
                }
            }
            if (atime > 0) {
                jq(this).prepend('<font color="#000000"><b>[' + diffTime(new Date(atime),new Date()) + '] </b></font>')
            }
        })
    }

})();

// https://www.jianshu.com/p/bdda96a1dcd8
function diffTime(startDate,endDate) {
    var diff=endDate.getTime() - startDate;//.getTime();//时间差的毫秒数

    //计算出相差天数
    var days=Math.floor(diff/(24*3600*1000));

    //计算出小时数
    var leave1=diff%(24*3600*1000);    //计算天数后剩余的毫秒数
    var hours=Math.floor(leave1/(3600*1000));
    //计算相差分钟数
    var leave2=leave1%(3600*1000);        //计算小时数后剩余的毫秒数
    var minutes=Math.floor(leave2/(60*1000));

    //计算相差秒数
    var leave3=leave2%(60*1000);      //计算分钟数后剩余的毫秒数
    var seconds=Math.round(leave3/1000);

    var returnStr = seconds + "秒前";
    if(minutes>0) {
        returnStr = minutes + "分钟前";//+ returnStr;
    }
    if(hours>0) {
        returnStr = hours + "小时前";// + returnStr;
    }
    if(days>0) {
        returnStr = days + "天前" ;//+ returnStr;
    }
    return returnStr;
}