B站显示up关注时间
// ==UserScript==
// @name B站显示up关注时间
// @namespace bc5
// @version 0.2
// @description B站 bilibili 哔哩哔哩 用户主页显示自己何时关注此用户
// @author C5732
// @match https://space.bilibili.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @grant none
// @run-at document-end
// ==/UserScript==
//时间戳转换为YYYY-MM-DD形式
function timestamp2time(stamp){
var date = new Date(stamp*1000);
var year = date.getFullYear()+"-";
var month = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
var day = date.getDate();
return (year + month + day);
}
var url = 'https://api.bilibili.com/x/space/acc/relation?mid=';//api地址
var pattern = new RegExp('([0-9]+)','gim');//正则规则
var uid = window.location.href.match(pattern)[0];//从当前url中截取UP主uid
url += uid;//拼接
var mtime = 0;//用来储存时间戳
var sw = true;
//一会将要插入的元素
let box=document.createElement("span");
box.innerText="";
box.style.cssText='color: rgb(255, 255, 255);font-size: 12px;display: inline-block;width: max-content;line-height: 16px;vertical-align: middle;padding: 1px 5px;background-color: #02b5da;border-radius: 8px;margin: 0 3px;';
//进行请求获取数据
var httpRequest = new XMLHttpRequest();
httpRequest.open("GET",url,true);
httpRequest.withCredentials = true;
httpRequest.send();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
var json = httpRequest.responseText;//获取到json字符串
console.log(json);
var res = JSON.parse(json);
if(res.code==0&&res.data.relation.attribute!=0&&res.data.relation.attribute!=128){
mtime = res.data.relation.mtime;
console.log("获取成功" + mtime + "分隔" + timestamp2time(mtime));
box.innerText=timestamp2time(mtime)+"关注";
}else{
sw = false;
console.log("获取失败");
}
}
};
//回调函数
let observer = new MutationObserver(
(mutationRecord, mutationObserver) => {
if(sw){
console.log('body had mutated!!!')
let inner = document.getElementsByClassName("h-viplabel")[0];
inner.parentNode.insertBefore(box,inner);
}
},
)
//监控目标是否子节点有变化
let target = document.querySelector(".h-basic");
observer.observe(document.body, { childList: true })