油猴脚本 在B站关注列表显示UP主最后更新时间

基本原理

使用自带的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);
}) ();
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值