Greasy Fork

Greasy Fork is available in English.

原神玩家指示器升级版

B站评论区自动标注米哈游玩家,依据动态给B站用户打分,基于laupuz-xu的代码

当前为 2022-09-12 提交的版本,查看 最新版本

// ==UserScript==
// @name         原神玩家指示器升级版
// @namespace    www.cber.ltd
// @version      0.8.0
// @description  B站评论区自动标注米哈游玩家,依据动态给B站用户打分,基于laupuz-xu的代码
// @author       urlz7
// @match        https://www.bilibili.com/video/*
// @match        https://space.bilibili.com/*/dynamic*
// @match        https://t.bilibili.com/*
// @match        https://www.bilibili.com/read/*
// @icon         https://static.hdslb.com/images/favicon.ico
// @connect      bilibili.com
// @grant        GM_xmlhttpRequest
// @license MIT
// @run-at document-end
// ==/UserScript==

(function() {
    'use strict';

    const sensitivity = 15 // 灵敏度

    const keywords_other = {
        "op": -3,
        "原批": -3,
        "原p": -3,
        "叩": -3,
        "欧泡": -3,
        "哦泡": -3,
        "糇": -3,
        "贱长": -3,
        "米孝子": -3,
        "mxz": -3,
        "数一数二": -1,
        "烧鸡": -1,
        "米学长": -1,
        "米卫兵": -1,
        "mvb": -1,
        "mwb": -1,
        "绝区零": 1,
        "#miHoYo": 1,
        "米黑": 1,
        "魔怔人": 1,
        "🐒": 2,
        "🐒吧": 2,
        "mzr": 2,
        "赛批": 3,
        "赛p": 3,
        "单机批": 3,
        "单机p": 3,
        "百万塞尔达": 3
    }
    const keywords_beng = {
        "崩坏3": 1,
        "舰长": 1,
        "律者": 1
    }
    const keywords_yuan = {
        "原神": 1,
        "须弥": 1,
        "稻妻": 1,
        "雷电将军": 1,
        "钟离": 1,
        "神子": 1,
        "刻晴": 1,
        "万叶": 1,
        "可莉": 1,
        "提瓦特": 1
    }

    const keywords = {...keywords_other, ...keywords_beng, ...keywords_yuan}
    const cache = {}

    const blog = 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space?&host_mid='
    const is_new = document.getElementsByClassName('item goback').length != 0 // 检测是不是新版

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

    const get_comment_list = () => {
        if (is_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')
        }
    }

    console.log(is_new)

    console.log("正常加载")
    let _ = setInterval(()=>{
        let commentlist = get_comment_list()
        if (commentlist.length != 0){
            commentlist.forEach(c => {
                let pid = get_pid(c)
                if (pid in cache) {
                    if (c.className == "user") {
                        c = c.firstChild;
                    }
                    if (c.textContent.endsWith("】") === false) {
                        c.append(cache[pid])
                    }
                    return
                }
                //console.log(pid)
                let blogurl = blog + pid
                let request = (calc, num_iter, more, off) => {
                    if (!more || num_iter == 0) {
                        if (!(pid in cache)) {
                            let normalize = x => 1/(1+Math.exp(-x)) // 归一化
                            let round = x => Math.ceil(x * 100)
                            let stars = (x, n) => "★".repeat(Math.ceil(x*n))+"☆".repeat(n-Math.ceil(x*n))
                            let yuan = (2*normalize(sensitivity*calc.count1/calc.num_posts) - 1) || 0
                            let beng = (2*normalize(sensitivity*calc.count2/calc.num_posts) - 1) || 0
                            let zonghepingfen = normalize(sensitivity*calc.score/calc.num_posts || 0)
                            let elem = (Math.abs(zonghepingfen - 0.5) > 0.02 && (yuan >= 0.5 || beng >= 0.5)) ? `【原:${stars(yuan, 5)} 崩:${stars(beng, 5)} 综合评分:${Math.ceil(zonghepingfen*100)}】` : ""
                            //let elem = `【${round(yuan)} ${round(beng)} ${round(zonghepingfen)}】`
                            cache[pid] = elem
                        }
                        return
                    }
                    GM_xmlhttpRequest({
                        method: "get",
                        url: off ? blogurl + "&offset=" + off : blogurl,
                        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){
                            if (res.status === 200){
                                //console.log('成功')
                                let {data: {has_more, items, offset}} = JSON.parse(res.response)
                                items.forEach(item => {
                                    let st = ""
                                    let findStringsWithKey = (obj, key) => {
                                        if (obj instanceof Array) {
                                            for (let i = 0; i < obj.length; i++) {
                                                findStringsWithKey(obj[i], key)
                                            }
                                        } else if (obj instanceof Object) {
                                          for (let prop in obj) {
                                            if (prop == key && typeof obj[prop] == "string") {
                                                st += obj[prop]
                                            }
                                            if (obj[prop] instanceof Object || obj[prop] instanceof Array) {
                                                findStringsWithKey(obj[prop], key)
                                            }
                                          }
                                        }
                                    }
                                    findStringsWithKey(item, "orig_text")
                                    Object.keys(keywords_yuan).forEach(k => {
                                        if (st.includes(k)) calc.count1 += Math.max(0, keywords_yuan[k])
                                    })
                                    Object.keys(keywords_beng).forEach(k => {
                                        if (st.includes(k)) calc.count2 += Math.max(0, keywords_beng[k])
                                    })
                                    Object.keys(keywords).forEach(k => {
                                        if (st.includes(k)) calc.score += keywords[k] * keywords[k]
                                    })
                                    calc.num_posts++
                                })
                                request(
                                    calc,
                                    num_iter - 1,
                                    has_more,
                                    offset
                                )
                            } else{
                                console.log('失败')
                                console.log(res)
                            }
                        },
                    });
                }
                request({count1: 0, count2: 0, score: 0, num_posts: 0}, 6, true);
            });
        }
    }, 4000)
    })();