Skip to content

Commit

Permalink
feat: hide ad comment #133
Browse files Browse the repository at this point in the history
  • Loading branch information
festoney8 committed Sep 20, 2024
1 parent 298340c commit fc0e679
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 7 deletions.
44 changes: 44 additions & 0 deletions src/filters/comment/dyn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const GM_KEYS = {
callUser: {
statusKey: 'dynamic-comment-call-user-filter-status',
},
isAD: {
statusKey: 'dynamic-comment-ad-filter-status',
},
},
white: {
root: {
Expand Down Expand Up @@ -532,6 +535,47 @@ if (isPageDynamic()) {
check(true)
},
}),
// 过滤 带货评论
new CheckboxItem({
itemID: GM_KEYS.black.isAD.statusKey,
description: '过滤 带货评论 (实验功能 需刷新)',
enableFunc: () => {
fetchHook.addPostFn(
async (
input: RequestInfo | URL,
init: RequestInit | undefined,
resp?: Response,
): Promise<Response | void> => {
if (!resp) {
return
}
if (
typeof input === 'string' &&
init?.method?.toUpperCase() === 'GET' &&
input.includes('api.bilibili.com/x/v2/reply/wbi/main')
) {
try {
const respData = await resp.clone().json()
const msg = respData?.data?.top?.upper?.content?.message
if (msg && /b23\.tv\/mall-|领券|gaoneng\.bilibili\.com/.test(msg)) {
respData.data.top = null
respData.data.top_replies = null
const newResp = new Response(JSON.stringify(respData), {
status: resp.status,
statusText: resp.statusText,
headers: resp.headers,
})
return newResp
}
} catch {
return resp
}
return resp
}
},
)
},
}),
]
dynamicPageCommentFilterGroupList.push(new Group('comment-type-filter-group', '评论区 按类型过滤', typeItems))

Expand Down
45 changes: 45 additions & 0 deletions src/filters/comment/space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Group } from '../../components/group'
import { ButtonItem, CheckboxItem, NumberItem } from '../../components/item'
import { WordList } from '../../components/wordlist'
import settings from '../../settings'
import fetchHook from '../../utils/fetch'
import { debugCommentFilter as debug, error } from '../../utils/logger'
import { isPageSpace } from '../../utils/pageType'
import { showEle, waitForEle } from '../../utils/tool'
Expand Down Expand Up @@ -41,6 +42,9 @@ const GM_KEYS = {
callUser: {
statusKey: 'dynamic-comment-call-user-filter-status',
},
isAD: {
statusKey: 'video-comment-ad-filter-status',
},
},
white: {
root: {
Expand Down Expand Up @@ -555,6 +559,47 @@ if (isPageSpace()) {
check(true)
},
}),
// 过滤 带货评论
new CheckboxItem({
itemID: GM_KEYS.black.isAD.statusKey,
description: '过滤 带货评论 (实验功能 需刷新)',
enableFunc: () => {
fetchHook.addPostFn(
async (
input: RequestInfo | URL,
init: RequestInit | undefined,
resp?: Response,
): Promise<Response | void> => {
if (!resp) {
return
}
if (
typeof input === 'string' &&
init?.method?.toUpperCase() === 'GET' &&
input.includes('api.bilibili.com/x/v2/reply/wbi/main')
) {
try {
const respData = await resp.clone().json()
const msg = respData?.data?.top?.upper?.content?.message
if (msg && /b23\.tv\/mall-|领券|gaoneng\.bilibili\.com/.test(msg)) {
respData.data.top = null
respData.data.top_replies = null
const newResp = new Response(JSON.stringify(respData), {
status: resp.status,
statusText: resp.statusText,
headers: resp.headers,
})
return newResp
}
} catch {
return resp
}
return resp
}
},
)
},
}),
]
spacePageCommentFilterGroupList.push(new Group('comment-type-filter-group', '评论区 按类型过滤', typeItems))

Expand Down
44 changes: 44 additions & 0 deletions src/filters/comment/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const GM_KEYS = {
callUser: {
statusKey: 'video-comment-call-user-filter-status',
},
isAD: {
statusKey: 'video-comment-ad-filter-status',
},
},
white: {
root: {
Expand Down Expand Up @@ -535,6 +538,47 @@ if (isPageVideo() || isPageBangumi() || isPagePlaylist()) {
check(true)
},
}),
// 过滤 带货评论
new CheckboxItem({
itemID: GM_KEYS.black.isAD.statusKey,
description: '过滤 带货评论 (实验功能 需刷新)',
enableFunc: () => {
fetchHook.addPostFn(
async (
input: RequestInfo | URL,
init: RequestInit | undefined,
resp?: Response,
): Promise<Response | void> => {
if (!resp) {
return
}
if (
typeof input === 'string' &&
init?.method?.toUpperCase() === 'GET' &&
input.includes('api.bilibili.com/x/v2/reply/wbi/main')
) {
try {
const respData = await resp.clone().json()
const msg = respData?.data?.top?.upper?.content?.message
if (msg && /b23\.tv\/mall-|领券|gaoneng\.bilibili\.com/.test(msg)) {
respData.data.top = null
respData.data.top_replies = null
const newResp = new Response(JSON.stringify(respData), {
status: resp.status,
statusText: resp.statusText,
headers: resp.headers,
})
return newResp
}
} catch {
return resp
}
return resp
}
},
)
},
}),
]
videoPageCommentFilterGroupList.push(new Group('comment-type-filter-group', '评论区 按类型过滤', typeItems))

Expand Down
31 changes: 24 additions & 7 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class FetchHook {
// 根据input和init对input进行预处理
private preFnArr: ((input: RequestInfo | URL, init: RequestInit | undefined) => RequestInfo | URL)[] = []
// 根据input,init,resp做返回resp前的后处理, 如克隆resp
private postFnArr: ((input: RequestInfo | URL, init: RequestInit | undefined, resp?: Response) => void)[] = []
private postFnArr: ((
input: RequestInfo | URL,
init: RequestInit | undefined,
resp?: Response,
) => Response | void | Promise<Response | void>)[] = []

private constructor() {
try {
Expand All @@ -31,7 +35,13 @@ class FetchHook {
this.preFnArr.push(fn)
}

addPostFn(fn: (input: RequestInfo | URL, init: RequestInit | undefined, resp?: Response) => void) {
addPostFn(
fn: (
input: RequestInfo | URL,
init: RequestInit | undefined,
resp?: Response,
) => Response | void | Promise<Response | void>,
) {
this.postFnArr.push(fn)
}

Expand All @@ -47,13 +57,20 @@ class FetchHook {
return origFetch(input, init)
}
// 获取resp
const resp = await origFetch(input, init)
let resp = await origFetch(input, init)
const origResp = resp.clone()
try {
// 后处理
this.postFnArr.forEach((fn) => {
fn(input, init, resp)
})
} catch {}
for (const fn of this.postFnArr) {
const ans = await fn(input, init, resp)
if (ans) {
resp = ans
}
}
} catch (err) {
error('fetch hook postFnArr', err)
return origResp
}
return resp
}
}
Expand Down

0 comments on commit fc0e679

Please sign in to comment.