原神/明日方舟/王者荣耀玩家指示器(可扩展/全平台) - bilibili.com

2022/9/11 16:25:30

目前為 2022-09-11 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        原神/明日方舟/王者荣耀玩家指示器(可扩展/全平台) - bilibili.com
// @namespace   Violentmonkey Scripts
// @match       https://www.bilibili.com/video/*
// @grant       none
// @version     1.03
// @author      -
// @description 2022/9/11 16:25:30
// @grant        GM_xmlhttpRequest
// @license MIT
// ==/UserScript==
(function () {
    const bili_new = document.getElementsByClassName('item goback').length != 0;
    const bili_url = 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space?&host_mid='
    const match = [{
        name: '[原神玩家]',
        color: "red",
        keyword: ['原神', '刻晴', '丘丘人', '雷电将军']
    },
    {
        name: '[明日方舟玩家]',
        color: "orange",
        keyword: ['明日方舟']
    },
    {
        name: '[王者荣耀玩家]',
        color: "blue",
        keyword: ['王者荣耀']
    }
    ]
    const isOk = new Set()
    console.log('ok')
    const bili_get_comment_list = () => {
        if (bili_new) {
            let lst = new Set()
            for (let c of document.getElementsByClassName('user-name')) {
                lst.add(c)
            }
            for (let c of document.getElementsByClassName('sub-user-name')) {
                lst.add(c)
            }
            return lst
        } else {
            return document.getElementsByClassName('user')
        }
    }
    const get_pid = (c) => {
        if (bili_new) {
            return c.dataset['userId']
        } else {
            return c.children[0]['href'].replace(/[^\d]/g, "")
        }
    }

    const config = {
        attributes: true,
        childList: true,
        subtree: true
    };
    var bili_match = 'comment-list '
    const callback = function (mutationsList, observer) {
        for (let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                if (mutation.target.className.toString() == bili_match) {
                    console.log("评论增加");
                    let bgcl = bili_get_comment_list()

                    bgcl.forEach(c => {
                        if (!isOk.has(c)) {
                            isOk.add(c)
                            let pid = get_pid(c);
                            GM_xmlhttpRequest({
                                method: "get",
                                url: bili_url + pid,
                                data: '',
                                headers: {
                                    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
                                },
                                onload: function (res) {
                                    let st = JSON.stringify(JSON.parse(res.response)
                                        .data)
                                    match.forEach(m => {
                                        for (let i = 0; i < m['keyword'].length; i++) {
                                            if (st.includes(m['keyword'][i])) {
                                                let toAppend = document.createElement("DIV");
                                                toAppend.style.color = m['color'];
                                                toAppend.style.display = 'inline-block'
                                                toAppend.innerHTML = m['name'];
                                                if (bili_new) {
                                                    c.append(toAppend);
                                                } else {
                                                    c.children[0].append(toAppend);
                                                }
                                                break;
                                            }
                                        }

                                    })


                                },
                            });
                        }

                    });
                }

            }
        }
    }
    const observer = new MutationObserver(callback);
    if (!bili_new) {
        observer.observe(document.getElementsByClassName('common')[0], config);
    } else {
        bili_match = 'reply-list';
        observer.observe(document.getElementById('comment'), config);

    }
})();