Bilibili - 不再白嫖

打开视频自动点赞

目前為 2021-08-16 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Bilibili - 不再白嫖
// @namespace    top.qwq123.scripts.BilibiliAutoLike
// @version      0.2
// @description  打开视频自动点赞
// @author       XcantloadX
// @run-at       document-end
// @icon         https://static.hdslb.com/images/favicon.ico
// @match        *://www.bilibili.com/video/*
// @match        *://t.bilibili.com
// @require      https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js
// @license      MIT License
// ==/UserScript==

(function() {
    const AutoLike = {
        settings: {
            enableVideoPage: true, //是否启用视频自动点赞(true=启用,false=禁用,下同)
            enableFollowingPage: false //是否启用动态自动点赞
        },

        init: function(){
            if(this.settings.enableFollowingPage && window.location.host === "t.bilibili.com"){
                this.initFollowing();
            }
            else if(this.settings.enableVideoPage && window.location.host === "bilibili" && window.location.pathname.indexOf("video") > 0){
                $(this.initVideo);
            }
        },

        //动态页面
        initFollowing: function(){
            let likes = []; //待点赞按钮
            let observer = new MutationObserver(function(changes){
                changes.forEach(function(change){
                    if(change.type != "childList")
                        return;

                    if($(change.target).hasClass("card")){ //判断是否是动态卡片
                        let likeBtn = $(change.target).find(".button-bar").find(".custom-like-icon")[0];
                        if(typeof(likeBtn) == "undefined" || $(likeBtn).hasClass("zan-hover")) //未找到按钮或已赞
                            return;
                        if(!likes.includes(likeBtn)) //避免重复添加
                            likes.push(likeBtn);
                    }
                });

            });
            observer.observe(document.body, {attributes: true, childList: true, subtree: true});
            window.setInterval(function(){
                if(likes.length > 0){
                    $(likes.shift()).click();
                    console.log("已赞");
                }
            }, 1000); //为了避免太快,采用队列的方式逐个点赞
        },

        //视频页面
        initVideo: function(){
            let liked = false;
            let oldPath = "";
    
            //刷新检测
            window.setInterval(function(){
                if(location.pathname != oldPath){
                    oldPath = location.pathname;
                    window.setTimeout(doLike, 5000);
                }
            });
    
            function doLike(){
                let like = undefined;
                if($(".ops .like").length > 0)
                    like = $(".ops .like");
                else
                    return;
    
                if(!$(like).hasClass("on")){
                    $(like).click();
                    $(like).addClass("on"); //防止太卡
                    liked = true;
                }
            }
        }
    };
    AutoLike.init();
})();