GD_Slime的bilibili终极成分指示器

融合了动态关键字、关注列表、粉丝牌列表, 并使用反作弊和权重系统的终极成分指示器.

目前为 2022-09-16 提交的版本。查看 最新版本

// ==UserScript==
// @name         GD_Slime的bilibili终极成分指示器
// @namespace    GD_Slime
// @version      0.1.2
// @description  融合了动态关键字、关注列表、粉丝牌列表, 并使用反作弊和权重系统的终极成分指示器. 
// @author       GD_Slime
// @match        https://www.bilibili.com/video/*
// @match        https://t.bilibili.com/*
// @match        https://space.bilibili.com/*
// @match        https://www.bilibili.com/read/*
// @connect      bilibili.com
// @grant        GM_xmlhttpRequest
// @license MIT
// @run-at document-end
// ==/UserScript==

(function() {
        'use strict';
        console.log('【GD_Slime的终极成分指示器】加载成功...')

        // 自定义设置
        const queryFollowSwitch = false // 是否开启查询关注(开启之后结果更加精准, 但不稳定, 非常容易被b站限制)
        const dynamicQueryTimes = 3 // 查询用户动态数, 1次是12条动态(多于3次会非常慢!)
        const antiCheatNum = 3 // 反作弊指数, 即当用户在同一动态中发送超过此数目的 不同成分的关键字 时, 将会被惩罚
        const weightThreshold = 5 // 权重阈值, 计算用户权重后如果大于等于该值才会添加tag

        // 成分, 可自定义
        const match = [
            {
                name: '【 原批⭕ 】',
                color: '#FF0000',
                keywords: ['#原神#', '刻晴', '丘丘人', '雷电将军', '派蒙', '胡桃', '神里绫华', '达拉丽娜', '钟离'],
                UIDs: [401742377, 450905062, 472729452,  // 赫萝的苹果(原神大up), 莴苣某人(原神大up)
                    653768, 1773346] // 原神官方, 原神官方客服, 原神官方运营
            }, 
            {
                name: '【 农批👨‍🌾 】',
                color: '#FF0000',
                keywords: ['#王者荣耀#', '王者', '元歌', '李信', '宫本武藏', '百里守约', '马可波罗', '娜可露露'],
                UIDs: [392836434, 57863910, // 哔哩哔哩王者荣耀赛事, 王者荣耀官方,
                    13221028, 108569350]  //迷茫小树叶, 梦泪, 
            },
            {
                name: '【 A畜🅰️ 】',
                color: '#FF0000',
                keywords: ["想到晚的瞬间","晚晚","嘉晚饭","乃贝","贝极星空间站","乃琳夸夸群","顶碗人",
                        "皇珈骑士","贝极星","乃宝","嘉心糖的手账本","嘉心糖","拉姐","然然","asoul",
                        "A-SOUL","水母","来点然能量","奶淇琳","珈乐","贝拉拉的717片星空", "嘉然我想对你说",
                        "嘉然今天吃什么", "向晚大魔王", "贝拉Kira", "乃琳Queen", "珈乐Carol"],
                UIDs: [703007996,672342685,672328094,672353429,672346917,351609538]
            },
            {
                name: '【 鸟批🐤 】',
                color: '#FF0000',
                keywords: ['文静', '千鸟Official', '明前奶绿', '奶绿',
                        '艾白', '一只修白勾', '修白勾',
                        '艾瑞思', '思思', '凜凜蝶凜',
                        '琳_千鸟Official', '王木木', 
                        'CoCo_千鸟Official'],
                UIDs: [667526012, 334537711, 1090010845, 1620923329, 1891728206, 553771121, //文静, 艾白, 思思, 木木, Co宝
                    2132180406, 1960682407, 1220317431, //奶绿, 白勾, 大蝶
                    7477307, 10797522, 1190365997, 758140, 5336308, 19268544, 6715117, 297285769, 56794789, 8834998, 1480514, 50025593, 37141, 29755625, 370160494, 213195775] 
                    // -~=$ 【鸟批名人堂】 $=~-
                    // GD_Slime, snawm, 文静大总管, 亮猪, 可达鸭, 张三, 懒羊羊, 模仿者, 纱雾里看花, 前列腺勇士, 乌桃茶, 心烧, 御坂io, 9191, 白帝圣剑甘道夫, 萌白
            },
            {
                name: '【 三畜🦶 】',
                color: '#009900',
                keywords: ["小狗说","玉桂幺幺340","三宝","3宝","巢友","巢畜","4畜","小狗生病","啵啵小狗341"],
                UIDs: [33605910] // 3姐本人
            },
            {
                name: '【 罕见🎌 】',
                color: '#FF0000',
                keywords: ["東雪蓮Official","东雪莲","莲宝"],
                UIDs: [1437582453] // 罕见本人
            },
            {
                name: '【 瞳畜🌟 】',
                color: '#FF0000',
                keywords: ["小星星","瞳宝","瞳子","瞳瞳","瞳星结","星瞳"],
                UIDs: [401315430, 2122506217] // 瞳子本人, 瞳子工具人
            },
            {
                name: '【 杰尼🐢 】',
                color: '#FF0000',
                keywords: ["脆鲨","娜娜米","海子姐"],
                UIDs: [434334701] // 海子姐本人
            },
            {
                name: '【 E畜🐛 】',
                color: '#FF0000',
                keywords: ["虞莫","柚恩","露早","莞儿","米诺"], 
                UIDs: [2018113152, 1811071010, 1795147802, 1669777785, 1875044092, 1778026586] //eoe官方, 剩下和keywords对应
            }
        ]
        const matchLength = match.length

        //三种方式的api
        const biliDynamicAPI = 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space?host_mid='
        // 找到了个更好的api 
        // https://account.bilibili.com/api/member/getCardByMid?mid=
        const biliFollowAPI = 'https://account.bilibili.com/api/member/getCardByMid?mid='
        // 老api 多次调用会被code 412限制
        // const biliFollowAPI = 'https://api.bilibili.com/x/relation/followings?ps=50&pn='
        const biliMedalAPI = 'https://api.live.bilibili.com/xlive/web-ucenter/user/MedalWall?target_id='

        // 用户代理
        const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
        
        // 缓存已经检查过的用户UID, 防止重复检查
        // TODO bug: 如果一个用户在评论区评论多次, 只有第一次才会上tag
        const checkedUID = []

        // 主循环函数: 参考三相之力 
        setInterval(async () => {
            try {
                console.log('【GD_Slime的终极成分指示器】主循环函数开始工作...')
                let commentList = getCommentList()
                if (commentList.length != 0) {
                    for(let user of commentList) {
                        var UID = getUID(user)
                        if (!checkedUID.includes(UID)) {
                            checkedUID.push(UID)
                            let Weights = new Array(3).fill(new Array(matchLength).fill(1))
                            await getDynamicWeights(UID).then((w) => {
                                Weights[0] = w
                            })
                            if (queryFollowSwitch) {
                                await getFollowWeight(UID).then((w) => {
                                    Weights[1] = w
                               })
                            }
                            await getMedalWeight(UID).then((w) => {
                                Weights[2] = w
                            })
                            //console.log(`${w1}, ${w2}, ${w3}`)
                            for(let i = 0; i < matchLength; i++) {
                                let totalWeight = 0
                                totalWeight += Weights[0][i] 
                                if(queryFollowSwitch) {totalWeight += Weights[1][i]} 
                                totalWeight += Weights[2][i] 
                                console.log(`${Weights[0]} | ${Weights[1]} | ${Weights[2]} | ${totalWeight} | ${UID}`)
                                if (totalWeight >= weightThreshold) {
                                    user.innerHTML += getTag(i)
                                    console.log("用户UID: %d 的Tag添加成功!", UID)
                                }
                            }
                        } 
                    }
                }
            } catch (error) {
                console.log('用户tag添加失败!')
            }
        }, 3000)
        
        // 检测是不是新版
        const is_new = document.getElementsByClassName('item goback').length != 0 

        // 获取所有评论
        const getCommentList = () => {
            if (is_new) {
                let list = new Set()
                for (let c of document.getElementsByClassName('user-name')) {
                    list.add(c)
                }
                for (let c of document.getElementsByClassName('sub-user-name')) {
                    list.add(c)
                }
                return list
            } else {
                return document.getElementsByClassName('user')
            }
        }

        // 获取指定用户的UID
        const getUID = (user) => {
            if (is_new) {
                return user.dataset['userId']
            } else {
                return user.children[0]['href'].replace(/[^\d]/g, "")
            }
        }

        // 拼接tag
        const getTag = (i) => {
            return "<b style='color: " + match[i].color + "'>" + match[i].name + "</b>"
        }

        // 请求
        function request(targetURL) {
            return new Promise((resolve, reject) => {
                let requestFunction = GM_xmlhttpRequest ? GM_xmlhttpRequest : GM.xmlHttpRequest
                requestFunction({
                    method: 'get',
                    url: targetURL,
                    data: '',
                    headers: {'userAgent': userAgent},
                    onload: (res) => {
                        resolve(res)
                    }, 
                    onerror: (err) => {
                        reject(err)
                    }
                })
            })
        }

        //判断给定字符串出现次数
        function getStrCount(scrStr, armStr) {
            var count=0;
            while(scrStr.indexOf(armStr) != -1 ) {
                scrStr = scrStr.replace(armStr,"")
                count++;    
            }
            return count;
        }

        // -=动态部分=-
        async function getDynamicWeights(UID) {
            try {
                // 权重
                let Weight = new Array(matchLength).fill(0)
                // 偏移值, 为本次获取的动态最后一条, 用于请求下一次动态
                let offset = 0
                for(let count = dynamicQueryTimes; count > 0; count--) {
                    if(offset == 0) {
                        var res = await request(biliDynamicAPI + UID)
                    } else {
                        var res = await request(biliDynamicAPI + UID + '&offset=' + offset)
                    }
                    if(res.status == 200) {
                        //console.log('获取UID: %d 动态成功!', UID)
                        if (JSON.parse(res.response).code == 0) {
                            // 检查是否还有动态
                            if (JSON.parse(res.response).data.has_more == false) {count = 0}
                            // 将偏移值改为本次获取的offset值
                            offset = JSON.parse(res.response).data.offset
                            let items = JSON.parse(res.response).data.items 
                            if (Object.keys(items).length != 0) {
                                items.forEach(c => {
                                    let dyn = JSON.stringify(c) // 本动态内容
                                    let antiCheat = new Array(matchLength).fill(false)
                                    for(let i = 0; i < matchLength; i++) {
                                        let keywords = match[i].keywords
                                        let count = 0
                                        for(let j = 0; j < keywords.length; j++) {
                                            // 统计字符串出现次数, 一个典型的文字动态会包含3个同样的关键词, 故除以3
                                            let strCount = getStrCount(dyn, keywords[j]) / 3
                                            if(strCount > 0) {
                                                Weight[i] += 1
                                                antiCheat[i] = true
                                                count += strCount
                                            }
                                        }
                                        // 反作弊, 防止反复刷同一成分的关键字
                                        if(count >= 7) { // [WARN] 别瞎调该数字 低了可能出现副作用
                                            Weight[i] -= 1.5 * (count - 7)
                                        } 
                                    }
                                    // 反作弊, 防止在同一动态中刷不同成分的关键字
                                    var cheatIndex = []
                                    var indexCount = 0
                                    for(let i = 0; i < antiCheat.length; i++) {
                                        if(antiCheat[i] == true) {indexCount++}
                                    }
                                    if(indexCount >= antiCheatNum) {
                                        for(let i = 0; i < antiCheat.length; i++) {
                                            if(antiCheat[i] == true) {cheatIndex.push(i)}
                                        }
                                    }
                                    for(let index of cheatIndex) {
                                        Weight[index] -= 2
                                    }
                                })
                            }
                        }
                    } 
                }
                return Weight
            } catch (error) {
                console.log('获取UID: %d 动态失败!', UID)
            }
        }

        // -=关注部分=-
        async function getFollowWeight(UID) {
            try {
                let Weight = new Array(matchLength).fill(0)
                // 定义缺省值
                let weightModifiedFlag = false
                let res = await request(biliFollowAPI + UID)
                if(res.status == 200) {
                    //console.log('获取UID: %d 关注成功!', UID)
                    if (JSON.parse(res.response).code == 0) {
                        let lists = JSON.parse(res.response).card.attentions
                        if (Object.keys(lists).length != 0) {
                            for(let uid of lists) {
                                for(let i = 0; i < matchLength; i++) {
                                    if(match[i].UIDs.includes(uid)) {
                                        Weight[i] += 2
                                        weightModifiedFlag = true
                                    }
                                }
                            }
                        }
                    } 
                }
                if(!weightModifiedFlag) {
                    // 缺省, 每个match里的对象权重为1
                    for(let c of Weight) {c = 1}
                }
                return Weight
            } catch (error) {
                console.log('获取UID: %d 关注失败!', UID)
            }
        }

        // -=粉丝牌部分=-
        async function getMedalWeight (UID) {
            try {
                let Weight = new Array(matchLength).fill(0)
                // 查看是否需要使用缺省值
                let weightModifiedFlag = false
                let res = await request(biliMedalAPI + UID)
                if (res.status == 200) {
                    //console.log('获取UID: %d 粉丝牌成功!', UID)
                    if (JSON.parse(res.response).code == 0) {
                        let data = JSON.parse(res.response).data
                        if (Object.keys(data.list).length != 0) {
                            data.list.forEach(medal => {
                                let upUID = medal.medal_info.target_id
                                let level = medal.medal_info.level
                                for(let i = 0; i < matchLength; i++) {
                                    if(match[i].UIDs.includes(upUID)) {
                                        if (level >= 1 && level <= 5) {
                                            Weight[i] += 2
                                        } else if (level >= 6 && level <= 10) {
                                            Weight[i] += 4
                                        } else if (level >= 11 && level <= 15) {
                                            Weight[i] += 6
                                        } else if (level >= 16 && level <= 20) {
                                            Weight[i] += 6
                                        } else if (level >= 21 && level <= 25) {
                                            Weight[i] += 10
                                        } else if (level > 25) {
                                            Weight[i] += 100 //铁定是该成分的
                                        }
                                        weightModifiedFlag = true
                                    }
                                }
                            })
                        }
                    }
                }
                if(!weightModifiedFlag) {
                    // 缺省, 每个match里的对象权重为1
                    for(let c of Weight) {c = 1}
                }
                return Weight
            } catch (error) {
                console.log('获取UID: %d 粉丝牌失败!', UID)
            }   
        }
    }
)();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址