在B站视频,动态评论区标注原神/明日方舟/王者荣耀玩家,可在配置里添加其它玩家以及修改匹配规则(动态,关注列表,视频),一键开启自动查询
当前为
// ==UserScript==
// @name 原神/明日方舟/王者荣耀玩家指示器(可扩展/全平台) - bilibili.com
// @namespace Violentmonkey Scripts
// @match https://*.bilibili.com/*
// @grant none
// @version 1.12
// @description 在B站视频,动态评论区标注原神/明日方舟/王者荣耀玩家,可在配置里添加其它玩家以及修改匹配规则(动态,关注列表,视频),一键开启自动查询
// @author fyb
// @license MIT
// ==/UserScript==
(function () {
const setting = {
automatic: 0,//是否开启自动查询,开启后自动在用户名后显示成分,关闭后需要点击用户名后的按钮查询(0为关闭,1为开启)
matchingDynamic: 1, //在动态里查询关键词(0为关闭,1为开启) 可与其他兼容
matchingVideo: 0, //在视频标题简介里匹配关键词(0为关闭,1为开启)可与其他兼容
matchingFollow: 0, //在关注列表里匹配关键词(0为关闭,1为开启)可与其他兼容
matchingFollowPage: 2, //查询关注列表页数,一页50个,最多查询五页(1-5),需先开启matchingFollow
noTagName: 1 //是否开启普通用户标签显示,只影响自动查询下的显示方式,手动查询该项永久为开启状态(0为关闭,1为开启)
}
const noTagName = { //没有被匹配到的用户的标签
name: '[普通用户]',
color: 'black'
}
const match = [ //匹配规则,name为用户标签,color为显示标签的颜色(支持16进制颜色码#fb7299),keyword为匹配关键词数组,follows为匹配关注列表数组
{
name: '[原神玩家]',
color: "red",
keyword: ['原神', '刻晴', '丘丘人', '雷电将军'],
follows: ['原神']
},
{
name: '[明日方舟玩家]',
color: "orange",
keyword: ['明日方舟'],
follows: ['明日方舟']
},
{
name: '[王者荣耀玩家]',
color: "blue",
keyword: ['王者荣耀'],
follows: ['哔哩哔哩王者荣耀赛事']
},
{
name: '[一个魂]',
color: "green",
keyword: ['嘉心糖', '顶碗人', '乃琳'],
follows: ['嘉然今天吃什么', '乃琳Queen', '珈乐Carol', '贝拉kira', '向晚大魔王']
}
]
let myCss = `
.userComponentBtn{
display:none;
border:1px solid #fb7299;
color:#fb7299;
cursor:default;
font-size:12px;
line-height:16px;
margin-left:5px;
}
.myCursor{
cursor:pointer;
}
.toHover:hover .userComponentBtn{
display:inline-block;
}
.progressBar
`
let css = document.createElement("style");
css.innerHTML = myCss;
document.body.appendChild(css);
const bili_new = document.getElementsByClassName('comment-m-v1').length + document.getElementsByClassName('item goback').length != 0;
console.log('原神/明日方舟/王者荣耀玩家指示器(可扩展/全平台)插件加载成功')
const bili_dyn_url = 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/space?&host_mid='
const bili_video_url = 'https://api.bilibili.com/x/space/arc/search?mid='
const bili_follow_url = 'https://api.bilibili.com/x/relation/followings?ps=50&pn='
const isAddBtn = new Set()
const isPlayer = new Set()
const userInfo = new Map()
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 addTag = (c, m) => {
let toAppend = document.createElement("DIV");
toAppend.style.color = m['color'] || 'black';
toAppend.style.display = 'inline-block'
toAppend.innerHTML = m['name'];
if (bili_new) {
c.append(toAppend);
} else {
c.children[0].append(toAppend);
}
}
const toMatchAll = (json, c) => {
if (!isPlayer.has(c)) {
match.forEach(m => {
for (let i = 0; i < m['keyword'].length; i++) {
if (json.includes(m['keyword'][i])) {
addTag(c, m)
isPlayer.add(c)
break;
}
}
})
}
}
const toMatchFollow = (arr, c) => {
if (!isPlayer.has(c)) {
if (arr.length == 0) {
addTag(c, {
name: '[隐藏关注]'
})
isPlayer.add(c)
}
else {
match.forEach(m => {
for (let i = 0; i < arr.length; i++) {
let a = m['follows'].filter((v) => arr[i]['uname'] == v)
if (a.length != 0) {
addTag(c, m)
isPlayer.add(c)
break;
}
}
})
}
}
}
async function ajax(url) {
const response = await window.fetch(url)
return await response.json()
}
const config = {
attributes: true,
childList: true,
subtree: true
};
var networkCount = 0;
const getUserInfo = async (c) => {
let result = {
dynamic: {},
video: {},
follow: []
}
let pid = get_pid(c);
if (!userInfo.has(pid)) {
if (bili_new) {
userInfo.set(pid, {})
}
if (setting.matchingDynamic == 1) {
result.dynamic = await ajax(bili_dyn_url + pid)
}
if (setting.matchingVideo == 1) {
result.video = await ajax(bili_video_url + pid)
}
if (setting.matchingFollow == 1) {
if (setting.matchingFollowPage >= 1 && setting.matchingFollowPage <= 5) {
for (let i = 1; i <= setting.matchingFollowPage; i++) {
let f = await ajax(bili_follow_url + i + '&vmid=' + pid)
if (f['data'] != null) {
result.follow = result.follow.concat(f['data']['list'])
}
}
}
}
userInfo.set(pid, result)
return result;
}
else {
return userInfo.get(pid)
}
}
const doAllMatch = (res, c) => {
toMatchAll(JSON.stringify(res.dynamic), c)
toMatchAll(JSON.stringify(res.video), c)
if (setting.matchingFollow == 1) {
toMatchFollow(res.follow, c)
}
}
const addQueryBtn = (c) => {
if (!isAddBtn.has(c)) {
isAddBtn.add(c);
let toAppend = document.createElement("DIV");
toAppend.innerHTML = '查成分'
toAppend.className = 'userComponentBtn myCursor'
toAppend.addEventListener("click", function () {
let _this = this;
_this.innerHTML = '查询中'
getUserInfo(c).then(function (res) {
doAllMatch(res, c)
if (!isPlayer.has(c)) {
addTag(c, noTagName)
}
_this.innerHTML = '查询完毕'
_this.className = 'userComponentBtn'
});
})
if (bili_new) {
c.parentNode.parentNode.className += ' toHover';
c.parentNode.append(toAppend);
} else {
c.className += ' toHover';
c.insertBefore(toAppend, c.children[1]);
}
}
}
var bili_match = ['comment-list ', 'reply-box']
const callback = function (mutationsList, observer) {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
for (let q = 0; q < bili_match.length; q++) {
if (mutation.target.className.toString() == bili_match[q]) {
let bgcl = bili_get_comment_list()
if (setting.automatic == 0) {
bgcl.forEach(c => {
addQueryBtn(c);
})
};
if (setting.automatic == 1) {
bgcl.forEach(c => {
getUserInfo(c).then(function (res) {
if (JSON.stringify(res) == '{}') {
return;
}
doAllMatch(res, c)
if (!isPlayer.has(c) && setting.noTagName == 1) {
addTag(c, noTagName)
isPlayer.add(c)
}
});
});
}
break;
}
}
}
}
}
const observer = new MutationObserver(callback);
if (window.location.pathname.indexOf('video') != -1 || window.location.pathname.indexOf('read') != -1) {
console.log("当前为视频页面")
if (!bili_new) {
observer.observe(document.body, config);
} else {
bili_match = ['reply-list', 'sub-reply-list'];
observer.observe(document.body, config);
}
}
if (window.location.hostname.indexOf('space') != -1 || window.location.hostname.indexOf('t.bilibili.com') != -1) {
console.log("当前为动态页面")
bili_match = ['comment-list has-limit', 'reply-box'];
observer.observe(document.body, config);
}
})();