虚拟主播粉丝成分

基于粉丝牌

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

// ==UserScript==
// @name         虚拟主播粉丝成分
// @namespace    dauabi
// @version      202240.9
// @description  基于粉丝牌
// @author       dauabi
// @match        https://www.bilibili.com/video/*
// @match        https://t.bilibili.com/*
// @match        https://space.bilibili.com/*
// @match        https://www.bilibili.com/read/*
// @match        https://live.bilibili.com/*
// @icon         https://static.hdslb.com/images/favicon.ico
// @connect      bilibili.com
// @connect      mongodb-api.com
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @license MIT
// @run-at document-end
// ==/UserScript==

(function () {
    'use strict';
    const colors = [
        '#5d968c',
        '#5f7a9c',
        '#8b7ea3',
        '#bf6787',
        '#c59f22',
        '#26695b',
        '#283b84',
        '#4c3888',
        '#a43359',
        '#fc7b27',
    ]

    const is_new = document.getElementsByClassName('item goback').length != 0 // 检测是不是新

    const get_uid = (user) => {
        if (is_new) {
            return user.dataset['userId']
        } else {
            return user.children[0]['href'].replace(/[^\d]/g, "")
        }
    }

    const get_comment_user_list = () => {
        if (is_new) {
            let lst = new Set()
            for (let user of document.getElementsByClassName('user-name')) {
                lst.add(user)
            }
            for (let user of document.getElementsByClassName('sub-user-name')) {
                lst.add(user)
            }
            return lst
        } else {
            return document.getElementsByClassName('user')
        }
    }

    console.log(is_new)
    console.log("正常加载")

    var user_html = {}

    var check = false
    const get_user = () => {
        if (!check) {
            let user = document.getElementsByClassName('h-user')[0]
            return user
        }
    }

    GM_addStyle(`
    .userinfo-add {
        font-size:    20px;
    }
    ` );
    const api_url = 'https://data.mongodb-api.com/app/data-pkkhw/endpoint/data/v1/action/find'
    const headers = {
        'Content-Type': 'application/json',
        'Access-Control-Request-Headers': '*',
        'api-key': 'AjqGd5HGDLdLyyeDFYabpTZtPPtMzndQQ9tLMNFDowjflHXjGOcBuywecc0YfQkO'
    }
    let _ = setInterval(() => {
        let user_list = get_comment_user_list()
        if (user_list.length != 0) {
            let users = []
            let k = 1
            user_list.forEach(user => {
                let uid = String(get_uid(user))
                if (!(Object.keys(user_html).includes(uid))) {
                    users.push(uid)
                    k = 2
                }
            })
            
            
            if (k == 2) {
                GM_xmlhttpRequest({
                    method: "post",
                    url: api_url,
                    data: '{"collection":"fans","database":"vt","dataSource":"Cluster0","filter": {"_id": {"$in": [' + users.join(',') + ']}}}',
                    headers: headers,
                    onload: function (res) {

                        if (res.status === 200) {
                            let fans = {}
                            JSON.parse(res.response).documents.forEach((v,_) => (fans[v['_id']] = v))

                            if (user_list.length != 0) {

                                user_list.forEach(user => {
                                    let uid = String(get_uid(user))
                                    if (!(Object.keys(user_html).includes(uid))) {
                                        user_html[uid] = ""
                                        let s = fans[uid]['data']
                                        if (s.length > 0) {
                                            user.innerHTML += " [ "
                                        }
                                        for (let i = 0; i < s.length; ++i) {
                                            if (i > 4) {
                                                break
                                            }
                                            let c = Math.floor(s[i][2] / 4)
                                            user.innerHTML += '<a data-usercard-mid="' + s[i][0] + '" href="//space.bilibili.com/' + s[i][0] + '" target="_black" class="name" style="color: ' + colors[c] + '">' + s[i][1] + '</a> '
                                        }
                                        if (fans[uid]['count'] > 5) {
                                            user.innerHTML += " ...<b>" + fans[uid]['count'] + "</b>"
                                        }
                                        if (s.length > 0) {
                                            user.innerHTML += " ]"
                                        }

                                    }
                                    else if (!user.innerHTML.replaceAll("'", '"').includes(user_html[uid])) {
                                        user.innerHTML += user_html[uid]
                                    }
                                });
                            }
                        }
                    },
                });
            }
        }
        let user = get_user()
        if (user && !check) {
            let lid = document.location.pathname.replace(/[^\d]/g, "")
            GM_xmlhttpRequest({
                method: "post",
                url: api_url + 'One',
                data: '{"collection":"livers","database":"vt","dataSource":"Cluster0","filter": {"_id": ' + lid + '}}',
                headers: headers,
                onload: function (res) {
                    if (res.status === 200) {
                        let info = JSON.parse(res.response)
                        info = info['document']['data']

                        let newHtml = document.createElement('div')
                        for (let i in info) {
                            let liver = info[i]
                            newHtml.innerHTML += '<a data-usercard-mid="' + liver[0] + '" href="//space.bilibili.com/' + liver[0] + '" target="_black" class="name"><b style="color: #ffffff" class="userinfo-add">' + i + ":" + liver[1] + '</b></a>' + " "
                        }
                        user.appendChild(newHtml)
                        check = true

                    }
                },
            });
        }
    }, 5000)
})();