B站稍后再看归位

它进收藏啦,被我搞出来啦,有什么好说哒

目前为 2021-05-19 提交的版本。查看 最新版本

// ==UserScript==
// @name         B站稍后再看归位
// @namespace    https://greasyfork.org/zh-CN/users/412840-newell-gabe-l
// @version      1.2.2
// @description  它进收藏啦,被我搞出来啦,有什么好说哒
// @author       Pronax
// @match        *://*.bilibili.com/*
// @exclude      *://live.bilibili.com/*
// @require      https://code.jquery.com/jquery-1.12.4.min.js
// @noframes
// @grant        GM_addStyle
// ==/UserScript==

(function () {
    'use strict';

    // 视频页面稍后再看按钮css
    GM_addStyle(".ops-watch-later.van-watchlater{top:4px;right:25px}.rigth-btn{margin-right:20px;}");
    // 进度条css
    GM_addStyle(".wl-progress-bar{width:0;height:3px;background-color:#ff74a5;position:absolute;bottom:0}");
    // 时长css
    GM_addStyle(".duration-tag[data-v-c05424d6]{left:4px;right:auto!important}");
    // 删除图标css
    GM_addStyle(".watchlist-item-img-del{display:none;position:absolute;right:4px;bottom:4px;padding:2px;width:20px;height:20px;-webkit-box-sizing:border-box;box-sizing:border-box;background:rgba(0,0,0,.5);border-radius:2px;cursor:pointer}.watchlist-item-img-del:hover{background:#000}");
    // 视频信息css
    GM_addStyle(".line-2{min-height:42px;}");

    var watchLaterBtnTimeout;
    var lastUpdateTiem;
    var watchLaterCountDown = 0;
    var watchLaterBtnCountDown = 0;

    var temp = '<div data-v-64b63b5f="" class="watch-later-video"><a data-v-37582e0a="" data-v-64b63b5f="" href="" target="_blank" class="header-video-card"><div data-v-37582e0a="" class="video-preview multiple-preview"><div data-v-c05424d6="" data-v-37582e0a="" class="video-card-img"><img data-v-c05424d6="" src="" alt="" class="default-img"><div data-v-c05424d6=""><div class="wl-progress-bar"></div><div data-v-c05424d6="" class="duration-tag"></div><div class="watchlist-item-img-del"><span class="svgicon-r"><svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M5.4 11.652a.878.878 0 001.757 0V6.46a1 1 0 00-.879-.99c-.49.082-.856.495-.878.99v5.192zm3.514 0c0 .485.393.877.879.877a.826.826 0 00.878-.877V6.46a.983.983 0 00-.878-1c-.494.083-.86.501-.879 1v5.192zm5.324-7.139H13.21v8.946a1.43 1.43 0 01-1.317 1.535H4.354a1.458 1.458 0 01-1.59-1.544V4.513h-.975a.877.877 0 01-.782-.877.78.78 0 01.672-.876H5.4v-.948a.702.702 0 01.782-.807h3.61a.703.703 0 01.783.781v.974h3.61a.782.782 0 01.809.876.877.877 0 01-.756.877z" fill="#FFF" fill-rule="evenodd"></path></svg></span></div></div></div></div><div data-v-37582e0a="" class="video-info"><div data-v-37582e0a="" title="" class="line-2"></div><div data-v-37582e0a="" class="info"><span data-v-37582e0a="" class="up"></span></div></div></a></div>';

    window.addAddWatchLaterBtn = function () {
        let btn = $(".ops-watch-later.van-watchlater");
        if (btn.length == 0 && watchLaterBtnCountDown < 15) {
            watchLaterBtnCountDown++;
            setTimeout(() => addAddWatchLaterBtn(), 100);
            return;
        } else if (watchLaterBtnCountDown >= 30) {
        } else {
            $(".rigth-btn").append($(".ops-watch-later.van-watchlater"));
            $(".more-ops-list li")[1].remove();
        }
    }

    window.getCookie = function (objName) {//获取指定名称的cookie的值 
        var arrStr = document.cookie.split("; ");
        for (var i = 0; i < arrStr.length; i++) {
            var temp = arrStr[i].split("=");
            if (temp[0] == objName) {
                return decodeURI(temp[1]);
            }
        }
    }

    window.loadWatchLater = function () {
        let c = $(".user-con.signin").children();
        if (c.length == 0 && watchLaterCountDown < 30) {
            watchLaterCountDown++;
            setTimeout(() => loadWatchLater(), 100);
            return;
        } else if (watchLaterCountDown >= 30) {
            console.log("加载稍后再看失败,你可能没有登录,或者你太卡了花了3秒都没有加载出页面");
            console.log("当然了,如果这个页面本身不适合加入稍后再看按钮,而你又看见了这条信息,恭喜你找到了一个bug,欢迎反馈给我");
        } else {
            let t = c[3];
            $(t).after($('<div class="item"><span><div class="mini-toview" tabindex="0"><span><span class="name">稍后再看</span></span><div role="tooltip" id="van-popover-watch-later" aria-hidden="true" class="van-popover van-popper van-popper-favorite" tabindex="0" style="width: 370px;transform-origin: center top;z-index: 2009;position: absolute;top: 40px !important;left: -270%;display: none;"><div data-v-e8d85714="" class="vp-container"><div data-v-e8d85714="" class="favorite-video-panel" style="padding-top: 0px;"><div data-v-e8d85714="" class="favorite-video-list watch-later-list"></div><div data-v-e8d85714 class="play-view-all"><a data-v-e8d85714 href="//www.bilibili.com/watchlater/#/list" target="_blank" class="play-all view">查看全部</a><a data-v-e8d85714 href="//www.bilibili.com/medialist/play/watchlater" target="_blank" class="play-all"><i data-v-e8d85714 class="bilifont bili-icon_dingdao_bofang"></i>播放全部</a></div></div></div></div></div></span></div>'));
            $(".mini-toview").click(() => {
                window.open("https://www.bilibili.com/watchlater/#/list");
            }).mouseenter(function () {
                clearTimeout(watchLaterBtnTimeout);
                watchLaterBtnTimeout = setTimeout(() => {
                    $("#van-popover-watch-later").stop().fadeIn(200);
                    if ((!lastUpdateTiem) || new Date().getTime() - lastUpdateTiem >= 30000) {
                        lastUpdateTiem = new Date().getTime();
                        $.ajax({
                            xhrFields: { withCredentials: true },
                            crossDomain: true,
                            url: "https://api.bilibili.com/x/v2/history/toview/web?jsonp=jsonp",
                            dataType: "json",
                            success: function (r) {
                                if (r.code == 0) {
                                    let list = r.data.list;
                                    for (let i = 0; i < list.length; i++) {
                                        let tar = $(temp);
                                        let a = tar.find(".header-video-card");
                                        a.attr("href", list[i].uri)
                                            .attr("data-bvid", list[i].bvid)
                                            .click(function (e) {
                                                if (e.ctrlKey) {
                                                    e.preventDefault();
                                                    window.open("https://www.bilibili.com/video/" + this.dataset.bvid);
                                                }
                                            });
                                        // https://www.bilibili.com/medialist/play/watchlater/
                                        // https://www.bilibili.com/video/
                                        tar.find(".default-img").attr("src", "https" + list[i].pic.substr(4) + "@112w_63h_1c_100q.webp");
                                        if (list[i].progress != 0) {
                                            tar.find(".wl-progress-bar").width(list[i].progress / list[i].duration * 100 + "%");
                                        }
                                        // todo 处理css
                                        if (list[i].videos != 1) {
                                            tar.find(".duration-tag").text(list[i].videos + "P");
                                        } else {
                                            let s = list[i].duration % 60;
                                            let m = Math.floor(list[i].duration / 60 % 60);
                                            let h = Math.floor(list[i].duration / 3600);
                                            tar.find(".duration-tag").text((h == 0 ? "" : h < 10 ? "0" + h + ":" : h + ":") + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s));
                                        }
                                        // jsonp: jsonp
                                        // bvids: BV1F4411G7zF
                                        // csrf: 76886df72948b0827073e4adf073156c
                                        tar.find(".watchlist-item-img-del").data("bvid", list[i].bvid);
                                        tar.find(".line-2").text(list[i].title).attr("title", list[i].title);
                                        tar.find(".up").text(list[i].owner.name);
                                        $(".watch-later-list:first").append(tar);
                                    }
                                    $(".watch-later-video").click(function (e) {
                                        e.stopPropagation();
                                    }).mouseenter(function () {
                                        $(this).find(".line-2").css("color", "#00a1d6");
                                        $(this).find(".watchlist-item-img-del").show();
                                    }).mouseleave(function () {
                                        $(this).find(".line-2").css("color", "");
                                        $(this).find(".watchlist-item-img-del").hide();
                                    });
                                    $(".watchlist-item-img-del").click(function (e) {
                                        let targ = this;
                                        e.preventDefault();
                                        $.ajax({
                                            url: "https://api.bilibili.com/x/v2/history/toview/del",
                                            type: "POST",
                                            xhrFields: { withCredentials: true },
                                            crossDomain: true,
                                            data: { "jsonp": "jsonp", "bvids": $(targ).data("bvid"), "csrf": getCookie("bili_jct") },
                                            dataType: "json",
                                            success: function (r) {
                                                if (r.code == 0) {
                                                    $(targ).parents(".watch-later-video").fadeOut(300);
                                                } else {
                                                    console.log("删除失败:", r.message);
                                                }
                                            },
                                            error: function (r) {
                                                console.log("删除失败:", r.message);
                                            }
                                        });
                                    });
                                    $(".play-view-all").click(function (e) { e.stopPropagation(); });
                                } else {
                                    console.log("加载稍后再看失败:", r.msg);
                                }
                            },
                            error: function (r) {
                                console.log("加载稍后再看失败:", r.msg);
                            },
                        });
                    }
                }, 200);
            }).mouseleave(function () {
                clearTimeout(watchLaterBtnTimeout);
                watchLaterBtnTimeout = setTimeout(() => {
                    $("#van-popover-watch-later").stop().fadeOut(100);
                }, 200);
            });
        }
    }

    setTimeout(() => {
        loadWatchLater();
        if (location.pathname.startsWith("/video/")) {
            addAddWatchLaterBtn();
        }
    }, 200);

})();