2022/9/11 16:25:30
当前为
// ==UserScript==
// @name 原神/明日方舟/王者荣耀玩家指示器(可扩展/全平台) - bilibili.com
// @namespace Violentmonkey Scripts
// @match https://*.bilibili.com/*
// @grant none
// @version 1.04
// @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 (window.location.pathname.indexOf('video') != -1) {
console.log("视频")
if (!bili_new) {
observer.observe(document.body, config);
} else {
bili_match = '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';
observer.observe(document.body, config);
}
})();