-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean-bilibili-comment.js
208 lines (193 loc) · 6.15 KB
/
clean-bilibili-comment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// ==UserScript==
// @name Clean Bilibili Comment
// @namespace https://meetinaxd.ltiex.com
// @version 0.3
// @description To be or not to be, that's a question.
// @author MeetinaXD
// @match https://www.bilibili.com/video/*
// @grant none
// @license MIT
// ==/UserScript==
// window.vd -> bilibili video info.
// :: title -> video title
// :: owner -> video author info.
// window.bbComment -> related to comment and reply
(function () {
// put your rules here...
const regExp = [
{ /* 回形针专用 */
author: 258150656,
title: /(回形针|PaperClip)+/,
filter: /(肉蛋奶|巴西雨林|黑子|买水|森林|人口|影响|黑名单|拉黑|雨林|原谅|敌军|毁掉|垄断|监控|打钱)+/,
init(){
console.log('检测到回形针📎视频');
$('.common').hide()
},
// set the flag as false to disable printing blocked comments
showBlocked: true
},
/* please put the global rule in the end */
{
// use regular expression **obj** or author mid to match
author: null,
// use RegExp **obj** to match
title: /\w+/,
// use RegExp **obj** to match
filter: /(肉蛋奶|巴西雨林|拉黑|敌军|毁掉|打钱|水军)+/,
init: "全局应用生效",
showBlocked: true,
// blockUser: true
}
]
const users = []
let timer = null
/**
* Block user by mid list
* [Reference] https://github.com/Hsury/Bilibili-Toolkit/blob/411307217b07ad37485e9642aab8761b4cc50eda/bilibili.py#L676-L690
* Thanks to ESWZY. https://github.com/ESWZY
*/
function blockUser(m) {
if (!Array.isArray(m))
m = [m]
const csrf = document.cookie.split(';').find(e => e.indexOf('bili_jct') !== -1).split('=')[1]
return new Promise((resolve, reject) => {
$.ajax({
url: 'https://api.bilibili.com/x/relation/batch/modify',
type: 'POST',
data: {
fids: m.join(','),
act: 5,
csrf,
re_src: 222
},
xhrFields: {
withCredentials: true
},
success: v => {
if (v.code === 0)
resolve()
else
reject(v.message)
},
error: reject
})
})
}
function doBlock() {
if (timer !== null)
clearTimeout(timer)
// 5 secs debounce
timer = setTimeout(() => {
blockUser(users)
.then(() => console.log('Block users:', users.join(',')))
.catch(e => console.error('Block user error', e))
timer = null
}, 5000)
}
let currentExp = null
function init() {
const commentBlock = "<span style='background: #eaeaea; color:#3e3e3e; font-size:14px'>This comment has been blocked</span>"
const urlBlock = "<span style='background: #eaeaea; color:red; font-size:14px'>This url has been blocked</span>"
// Comment and reply rendering method
const fn1 = window.bbComment.prototype._createMsgContent
const fn2 = window.bbComment.prototype._createSubMsgContent
const fn3 = window.bbComment.prototype._resolveJump
// Once the script handle the original render methods, the following objects will not be inited correctly.
window.bbComment.prototype.jumpReportIndex = 0
window.bbComment.prototype.jumpReport = {}
window.bbComment.prototype._createMsgContent = function (e) {
return handler(e, fn1)
}
window.bbComment.prototype._createSubMsgContent = function (e) {
return handler(e, fn2)
}
window.bbComment.prototype._resolveJump = function (e, t) {
return handler(e, fn3, t)
}
const handler = function (e, fn, extra = null) {
const exp = currentExp.filter
if (extra) {
let replace = ""
for (let k in extra){
if (exp.test(extra[k].title)){
extra[k] = {}
replace += urlBlock
}
}
return replace + fn.call(window.bbComment.prototype, e, extra)
}
let domText = commentBlock
let text = e.content.message
if (!exp.test(text)) {
domText = fn.call(window.bbComment.prototype, e)
} else {
if (currentExp.showBlocked) {
console.log('\t%c已过滤 >>> ', "color: orange", text)
}
if (currentExp.blockUser) {
users.push(e.mid)
doBlock()
}
}
return domText
}
}
const intervalId = setInterval(function () {
// init exp
if (window.vd && !currentExp) {
const vd = window.vd
const authorName = vd.owner.name
const authorMid = vd.owner.mid
const title = vd.title
function setExp(o) {
if (!o && typeof o.filter !== "[object RegExp]"){
console.error("[Clean Bilbili Comment] Your filter is illegal")
}
currentExp = o
}
for (let i = 0; i < regExp.length; i++) {
const exp = regExp[i];
if (exp.author) {
const type = typeof exp.author
if (type === "string" && authorName.indexOf(exp.author) === -1){
continue
}
if (type === "number" && authorMid !== exp.author){
continue
}
if (type === "[object RegExp]" && !exp.author.test(authorName)){
continue
}
setExp(exp)
break
}
if(exp.title){
const type = typeof exp.title
if (type === "string" && title.indexOf(exp.title) === -1){
continue
}
if (type === "[object RegExp]" && !exp.title.test(title)){
continue
}
setExp(exp)
break
}
}
// no match video, cancel.
if (!currentExp){
console.warn("[Clean Bilbili Comment] 无匹配条件")
clearInterval(intervalId)
}
}
if (currentExp && typeof $ !== 'undefined' && $('.comment').length !== 0 && !!window.bbComment && !!window.vd) {
clearInterval(intervalId)
init()
if (typeof currentExp.init === 'function'){
currentExp.init()
} else if (typeof currentExp.init === 'string') {
console.warn(currentExp.init)
}
console.warn("[Clean Bilbili Comment] Bilibili 评论区净化注入成功")
}
}, 100)
})();