B站评论区自动标记 tag,依据是动态里是否有对应的 keyword
目前為
// ==UserScript==
// @name 真三国无双指示器
// @namespace www.cber.ltd
// @version 0.6666
// @description B站评论区自动标记 tag,依据是动态里是否有对应的 keyword
// @author xulaupuz, xcl
// @match https://www.bilibili.com/video/*
// @match https://www.bilibili.com/bangumi/*
// @match https://space.bilibili.com/*/dynamic
// @match https://t.bilibili.com/*
// @icon https://static.hdslb.com/images/favicon.ico
// @connect bilibili.com
// @grant GM_xmlhttpRequest
// @license MIT
// @run-at document-end
// ==/UserScript==
// fork from: https://greasyfork.org/zh-CN/scripts/450720 感谢原作者的贡献
(function () {
'use strict';
const unknown = new Set()
const targetUserMap = new Map();
const otherUserSet = new Set();
// 使用指南,只要匹配到 keywords 里的任意一项,即标记为对应的 tag
const keywordsTagGroup = [
{
keywords: ['原神'],
tag: '原神玩家',
},
{
keywords: ['王者荣耀'],
tag: '农药玩家',
},
{
keywords: ['明日方舟', '舟游'],
tag: '舟游玩家',
}
];
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']
} else {
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("正常加载")
const getTagSpan = (text) => {
return ` <span style="background-color: #6bc047; color: #fff; border-radius:7px; font-size: 10px; padding: 0 4px;">${text}</span>`
}
const appendTags = (elem, tags = []) => {
if (!elem) return;
for (const tag of tags) {
elem.innerHTML += getTagSpan(tag);
}
}
let monitor = setInterval(() => {
let commentList = get_comment_list()
if (commentList.length != 0) {
// clearInterval(monitor)
commentList.forEach(c => {
// 同一个用户的重复的评论需要标记 tag
let pid = get_pid(c)
if (targetUserMap.has(pid)) {
const tags = targetUserMap.get(pid)?.tags ?? [];
if (c.innerHTML.includes('span') === false) {
appendTags(c, tags);
}
return
} else if (otherUserSet.has(pid)) {
// do nothing
return
}
unknown.add(pid)
let blogUrl = blog + pid
GM_xmlhttpRequest({
method: "get",
url: 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) {
let st = JSON.stringify(JSON.parse(res.response).data)
unknown.delete(pid)
const tags = [];
keywordsTagGroup.forEach(({ keywords, tag }) => {
for (const keyword of keywords) {
if (st.includes(keyword)) {
tags.push(tag);
break;
}
}
});
if (tags.length) {
appendTags(c, tags);
if (!targetUserMap.has(pid)) {
targetUserMap.set(pid, {
tags,
})
}
} else {
otherUserSet.add(pid);
}
} else {
console.log('失败')
console.log(res)
}
},
});
});
}
}, 4000)
})();