九月资源网-不以盈利为目的,仅仅为个人学习、研究或者欣赏。若未注明来源,请联系添加。如果侵犯到您的版权,请联系删除相关文章。

哔哩哔哩个人空间视频添加播放进度条js

技术类 Thor 来源:52破解-梦汐 1个月前 (11-09) 21次浏览 已收录 0个评论 扫描二维码
文章目录[隐藏]

前言

上次的是云端版这个是本地版的,不过可以利用油猴的OneDrive云备份达到云端储存播放记录(油猴)

哔哩哔哩播放进度条JS

文章目录[隐藏] 前言 代码 前言 哔哩哔哩的云端播放记录需要改本地保存才能正常使用。 代码 (functio […]

界面

代码

// ==UserScript==
// [url=home.php?mod=space&uid=170990]@name[/url]        哔哩哔哩 - beta
// [url=home.php?mod=space&uid=467642]@namespace[/url]   Violentmonkey Scripts
// [url=home.php?mod=space&uid=195849]@match[/url]       *://*.bilibili.com/*
// [url=home.php?mod=space&uid=1248337]@version[/url]     1.0
// [url=home.php?mod=space&uid=686208]@AuThor[/url]      -
// [url=home.php?mod=space&uid=609072]@grant[/url]       GM_setValue
// @grant       GM_getValue
// @require     [url=https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js]https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js[/url]
// ==/UserScript==
const Match = {
    Init: function () {
        var link = window.location.href
        var rules = this.Rules
        for (keyNmae in rules) {
            var focus = rules[keyNmae] //==[正则,[函数]]
            var result = RegExp(focus[0], "i").exec(link)
            if (result != null) {
                console.log("页面类型", keyNmae, " | ", "使用函数", focus[1]);
                for (let i = 0; i < focus[1].length; i++) {
                    focus[1][i].call(null, { result: result.slice(1,), rules: focus[0] })
                }
            }
        }
    },
    Rules: {//正则
        "Video": [".*://www.bilibili.com/video/([ab][v][a-z0-9]*).*", [RecordLastPlayTime]],//视频地址
        "Space": [".*://space.bilibili.com/.*", [AddPlayProgressUI]]//空间主页
    },
};
(function () { Match.Init() }());
function RecordLastPlayTime() {
    setInterval(function () {
        var Rresult = RegExp(".*://www.bilibili.com/video/([ab][v][a-z0-9]*).*", "i").exec(window.location.href)
        if (Rresult != null) {
            var node = []
            var element = document.querySelector('.bpx-player-ctrl-time-current')
            if (!!element) {
                node.push(element)
            }//播放进度
            var element = document.querySelector('.bpx-player-ctrl-time-duration')
            if (!!element) {
                node.push(element)
            }//视频时长
            if (node.length == 2) {
                var id = Rresult[1]
                //------------------------------------------------------
                var minute = node[0].innerHTML.split(":")
                var progress = (minute[0] * 60) + Math.trunc(minute[1])
                //------------------------------------------------------
                var minute = node[1].innerHTML.split(":")
                var duration = (minute[0] * 60) + Math.trunc(minute[1])
                //------------------------------------------------------
                var json = GM_getValue_local("config", id, {})
                if (!json.progress && !json.duration && !json.interaction) {
                    var type = id[0].toLowerCase == "a" ? "avid" : "bvid";
                    var httpRequest = new XMLHttpRequest();
                    httpRequest.open('GET', `[url=https://api.bilibili.com/x/web-interface/view?]https://api.bilibili.com/x/web-interface/view?[/url]${type}=${id}`, false);
                    httpRequest.send(null);
                    var json = JSON.parse(httpRequest.responseText);
                    var duration = json.data.duration
                    var interaction = json.data.rights['is_stein_gate']
                    if (interaction == 1) {
                        //console.log("该视频为互动视频", id, progress, duration);
                        GM_setValue_local("config", id, {
                            interaction: interaction
                        })
                    } else {
                        //console.log("首次记录", id, progress, duration);
                        GM_setValue_local("config", id, {
                            progress: progress,
                            duration: duration
                        })
                    }
                } else {
                    if (!json.interaction) {//不是互动视频才进行记录
                        var disparity = json.duration - duration
                        if (disparity > -5 && disparity < 5) {//这是哔哩哔哩的BUG//同一个视频差距时间不得超过5秒
                            if (progress > json.progress) {//避免被小数覆盖
                                GM_setValue_local("config", id, {
                                    progress: progress,
                                    duration: duration
                                })
                            }
                        }
                    } else {
                        //console.log(id,"该视频为互动视频,无法记录");
                    }
                }
            }
            //console.log(id, { progress: progress, duration: duration });
        }
    }, 1000)
}
function AddPlayProgressUI() {
    ObserveNewElements(".small-item.fakeDanmu-item", function (node) {
        var id = $(node).attr('data-aid')
        var json = GM_getValue_local("config", id, {})
        //console.log("config", id, json.progress, json.duration);
        if (!$.isEmptyObject(json)) {//判断JSON是否为空
            var progress_ui = document.createElement('div');
            $(progress_ui).attr('class', 'progress').attr('style', 'top: 0px;position: absolute;width: 100%;height: 4px;background-color: #909090;').html(`<div id="content" style="width: ${GetPercent(json.progress, json.duration)}%;height: 100%;background-color: #f00;"></div>`);
            $(node).find('.cover').append(progress_ui);
        }
    })
}
 
function GM_getValue_local(id, key, defaultValue) {
    var data = GM_getValue(id, {});
    return data[key] || defaultValue;
}
function GM_setValue_local(id, key, value) {
    var data = GM_getValue(id, {});
    data[key] = value;
    return GM_setValue(id, data);
}
function ObserveNewElements(selector, response, filter) {//selector,(node,mutation)=>{},filterRepetition=true)
    'use strict';
    if (!!selector) {
        let config = {
            childList: true,
            subtree: true
        };
        const mutationCallback = (mutationsList) => {
            for (let mutation of mutationsList) {
                let type = mutation.type;
                if (type == "childList") {
                    if (mutation.addedNodes.length != 0) {
                        var New_node = []
                        for (let index = 0; index < mutation.addedNodes.length; index++) {
                            var Nodes = $(mutation.addedNodes[index])
                            for (let Tindex = 0; Tindex < Nodes.length; Tindex++) {
                                if ($(Nodes[Tindex]).is(selector)) {
                                    New_node.push(Nodes[Tindex])
                                }
                            }
                            Nodes.find(selector).each(
                                function () {
                                    New_node.push(this)
                                }
                            )
                        }
                        if (New_node.length > 0) {
                            setTimeout((function (List) {
                                for (let Tindex = 0; Tindex < List.length; Tindex++) {
                                    if (filter) {
                                        if (List[Tindex]['OpenFilter']) {
                                            console.log("重复", List[Tindex]);
                                            continue;
                                        } else {
                                            List[Tindex]['OpenFilter'] = true
                                        }
                                    }
                                    response.call(mutation, List[Tindex], mutation)
                                }
                            }(New_node)), 300)
                        }
                    }
                }
            }
        };
        var observe = new MutationObserver(mutationCallback);
        observe.observe(document, config)
        return observe
    }
}
function GetPercent(num, total) {//取百分比
    num = parseFloat(num);
    total = parseFloat(total);
    if (isNaN(num) || isNaN(total)) {
        return 0;
    }
    return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00);
}

本文标题:哔哩哔哩个人空间视频添加播放进度条js
若文件有解压密码,默认为:www.sir8.cn
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的,不得将上述内容用于商业或者非法用途。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
如有侵权,请联系邮箱shidao144@gmail.com进行删除处理。
喜欢 (0)
[]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址