// ==UserScript== // @name 记录上次查看时间 // @version 0.4.0 // @match https://www.mcbbs.net/* // @author xmdhs // @license MIT // @description 记录上次查看时间。 // @namespace https://xmdhs.top // @require https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/idb-keyval/6.1.0/umd.min.js // @downloadURL none // ==/UserScript== (async function () { let tid = document.querySelector("#postlist .plc a[href^='forum.php?mod=viewthread&action=printable']") const customStore = idbKeyval.createStore('lastview-db-name', 'lastview-store-name'); let data = localStorage.getItem("lastview") if (data) { let o = JSON.parse(data) for (const key in o) { await idbKeyval.set(String(key), String(o[key]), customStore) } localStorage.removeItem("lastview") } try { let temp = new URLSearchParams(tid.href) tid = temp.get("tid") } catch (error) { console.debug(error); } if (tid > 0) { idbKeyval.set(String(tid), String(new Date().getTime()), customStore) } else { const dl = document.querySelectorAll("tbody[id^='normalthread_'") for (const d of dl) { const dom = d.querySelector(".xst") let link = dom["href"] if (!link) { continue } 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 = await idbKeyval.get(tid, customStore) } else { let b = new URLSearchParams(link) let tidtemp = b.get("tid") if (tidtemp != "") { atime = await idbKeyval.get(tidtemp, customStore) } } atime = new Number(atime) let lastTime; const tDom = d.querySelector("td.by > em > a > span[title]") if (tDom) { lastTime = Date.parse(tDom["title"]) } else { lastTime = Date.parse(d.querySelector("td.by > em > a").textContent) } if (atime > 0) { let d = document.createElement("span") d.innerHTML = `` d.querySelector("b").textContent = `[${diffTime(new Date(atime), new Date())}]` if (lastTime > atime) { d.querySelector("font").style.color = "red" } dom.insertBefore(d, dom.firstChild) } } } })(); // 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; }