基本原理
使用自带的jq请求api获取到数据后直接插入html中。为一个练习的脚本。
用途
好像也没啥用,就练练手。偶尔可以通过最后更新时间判断这个UP主是否基本不更新而取关。
完整代码如下:
// ==UserScript==
// @name 在关注列表显示UP主最后更新时间 - bilibili.com
// @namespace Violentmonkey Scripts
// @match *://space.bilibili.com/*/fans/follow*
// @grant none
// @version 1.0
// @author pipikun
// @description 2022/6/8 22:46:55
// ==/UserScript==
// 引用https://blog.csdn.net/qq_44242030/article/details/114623976
function myDate(value, type = 1) {
var time = new Date(value * 1000);
var year = time.getFullYear();
var month = time.getMonth() + 1;
var date = time.getDate();
var hour = time.getHours();
var minute = time.getMinutes();
var second = time.getSeconds();
month = month < 10 ? "0" + month : month;
date = date < 10 ? "0" + date : date;
hour = hour < 10 ? "0" + hour : hour;
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;
var arr = [
year + "-" + month + "-" + date,
year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second,
year + "年" + month + "月" + date,
year + "年" + month + "月" + date + " " + hour + ":" + minute + ":" + second,
hour + ":" + minute + ":" + second,
date + "天" + hour + "小时"
]
return arr[type];
}
function 更新一个li(e) {
try {
var up空间地址 = e.querySelector("a").href;
} catch (error) {
return;
}
var up主uid = parseInt(up空间地址.match(/\d+/g)[0]);
// 引用https://www.jquery123.com/jQuery.get/
$.get(`https://api.bilibili.com/x/space/arc/search?mid=${up主uid}&ps=30&tid=0&pn=1&keyword=&order=pubdate&jsonp=jsonp`).success(
(data, _) => {
// 引用https://www.cnblogs.com/yehuisir/p/14278537.html
var 当前时间 = Date.parse(new Date()) / 1000;
var 投稿最后更新时间 = data.data.list.vlist[0].created;
var html_text = `<p class=\"auth-description\">上次投稿时间:${myDate(投稿最后更新时间)}。</p>`;
e.querySelector("div.content > a").insertAdjacentHTML("afterend", html_text);
});
$.get(`https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history?host_uid=${up主uid}&offset_dynamic_id=0&need_top=1&platform=web`).success(
(data, _) => {
var 当前时间 = Date.parse(new Date()) / 1000;
var 动态最后更新时间 = data.data.cards[0].desc.timestamp;
var html_text = `<p class=\"auth-description\">上次动态时间:${myDate(动态最后更新时间)}。</p>`;
e.querySelector("div.content > a").insertAdjacentHTML("afterend", html_text);
});
};
function 更新up主最后更新时间() {
var ul_list = document.querySelector("#page-follows > div > div.follow-main > div.follow-content.section > div.content > ul.relation-list");
ul_list.childNodes.forEach((e) => {
更新一个li(e);
});
};
(function () {
'use strict';
console.log("Violentmonkey Scripts 在关注列表显示UP主最后更新时间");
// 等待li出现
var isDisplayNow = false;
var evenFunc;
var t2 = setInterval(function () {
if (document.querySelector("#page-follows > div > div.follow-main > div.follow-content.section > div.content > ul.relation-list > li")) {
if(!isDisplayNow)
{
console.log("bind");
isDisplayNow = true;
更新up主最后更新时间();
$("#page-follows > div > div.follow-main > div.follow-content.section > div.content > ul.relation-list").bind("DOMNodeInserted", evenFunc = function (e) {
更新一个li(e.target)
}
);
}
}else{
if(isDisplayNow)
{
console.log("unbind");
isDisplayNow = false;
$("#page-follows > div > div.follow-main > div.follow-content.section > div.content > ul.relation-list").unbind("DOMNodeInserted", evenFunc);
}
}
}, 1000);
}) ();