CKAutoLoader

[已弃用] CKAutoLoader用于在哔哩哔哩播放器页面延后加载脚本,参考了Pakku弹幕插件的加载方式。

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

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/405344/835114/CKAutoLoader.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         CKAutoLoader
// @namespace    blbljsloader.ckylin.site
// @version      0.2
// @author       CKylinMC
// @grant        unsafeWindow
// @license      GPLv3 License
// ==/UserScript==
if (!window.CKAutoLoader) {
    window.CKAutoLoader = {
        loaded: false,
        inited: false,
        loader_Started: false,
        commentDetecterTriggered: false,
        cblist: {},
        retry_count: 50,
        playerLoadedEvent: null,
        commentLoadedEvent: null,
        start: function () {
            if (!window.CKAutoLoader.loader_Started) {
                window.CKAutoLoader.loader();
            }
        },
        initEvent: function () {
            if (window.CKAutoLoader.inited) return;
            if (window.CKAutoLoader.playerLoadedEvent != null) return;
            window.CKAutoLoader.playerLoadedEvent = new Event("ckBilibiliPlayerLoaded");
            window.CKAutoLoader.commentLoadedEvent = new Event("ckBilibiliCommentLoaded");
        },
        reg: function (name, callback) {
            if (window.CKAutoLoader.loaded) {
                if (callback instanceof Function) {
                    callback();
                }
                return;
            }
            window.CKAutoLoader.cblist[name] = callback;
            window.CKAutoLoader.start();
        },
        canInject: function () {
            //参考pakku的检测加载机制
            let blplayer = document.querySelector("div.bilibili-player");
            if (blplayer && !blplayer.querySelector(".bilibili-player-auxiliary-area")) {
                blplayer = blplayer.closest("body");
            }
            if (blplayer) {
                var list_elem = blplayer.querySelector(".bilibili-player-danmaku, .player-auxiliary-danmaku-wrap")
            }
            if (!blplayer || !list_elem) {
                return false;
            }
            return true;
        },
        startCommentDetecter: function () {
            if (window.CKAutoLoader.commentDetecterTriggered) return;
            window.CKAutoLoader.commentDetecterTriggered = true;
            window.CKAutoLoader.commentDetecter();
        },
        commentDetecter: function () {
            let commentElement = document.querySelector("#comment");
            if (!commentElement) return;
            if (commentElement.hasAttribute("scrollshow") &&
                commentElement.getAttribute("scrollshow") == "true") {
                window.dispatchEvent(window.CKAutoLoader.commentLoadedEvent);
            } else {
                setTimeout(() => {
                    window.CKAutoLoader.commentDetecter()
                }, 1000);
            }
        },
        loader: function () {
            window.CKAutoLoader.initEvent();
            window.CKAutoLoader.loader_Started = true;
            window.CKAutoLoader.startCommentDetecter();
            console.log("CKAutoLoader: try inject...");
            if (!window.CKAutoLoader.canInject()) {
                if (window.CKAutoLoader.retry_count == undefined || --window.CKAutoLoader.retry_count <= 0) {
                    console.error("CKAutoLoader: Can NOT inject scripts.");
                    return;
                }
                setTimeout(function () {
                    window.CKAutoLoader.loader()
                }, 200);
                return;
            }
            window.dispatchEvent(window.CKAutoLoaderplayerLoadedEvent);
            window.CKAutoLoader.loaded = true;
            for (func in window.CKAutoLoader.cblist) {
                if (window.CKAutoLoader.cblist[func] instanceof Function) {
                    try {
                        window.CKAutoLoader.cblist[func]();
                    } catch (e) {
                        console.error("CKAutoLoader: Errored while call: " + func + e);
                    }
                } else {
                    console.error("CKAutoLoader: Can NOT call: " + func);
                }
            }
        },
    };
    window.CKAutoLoader.start();
}