diff --git a/src/filters/comment/dyn.ts b/src/filters/comment/dyn.ts index 4eedf943..dcc42139 100644 --- a/src/filters/comment/dyn.ts +++ b/src/filters/comment/dyn.ts @@ -42,6 +42,9 @@ const GM_KEYS = { callUser: { statusKey: 'dynamic-comment-call-user-filter-status', }, + isAD: { + statusKey: 'dynamic-comment-ad-filter-status', + }, }, white: { root: { @@ -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 => { + 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)) diff --git a/src/filters/comment/space.ts b/src/filters/comment/space.ts index 73714929..c3012a5e 100644 --- a/src/filters/comment/space.ts +++ b/src/filters/comment/space.ts @@ -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' @@ -41,6 +42,9 @@ const GM_KEYS = { callUser: { statusKey: 'dynamic-comment-call-user-filter-status', }, + isAD: { + statusKey: 'video-comment-ad-filter-status', + }, }, white: { root: { @@ -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 => { + 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)) diff --git a/src/filters/comment/video.ts b/src/filters/comment/video.ts index 4a423abc..a2a74f91 100644 --- a/src/filters/comment/video.ts +++ b/src/filters/comment/video.ts @@ -52,6 +52,9 @@ const GM_KEYS = { callUser: { statusKey: 'video-comment-call-user-filter-status', }, + isAD: { + statusKey: 'video-comment-ad-filter-status', + }, }, white: { root: { @@ -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 => { + 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)) diff --git a/src/utils/fetch.ts b/src/utils/fetch.ts index 81a2ebe4..502bbddc 100644 --- a/src/utils/fetch.ts +++ b/src/utils/fetch.ts @@ -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)[] = [] private constructor() { try { @@ -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, + ) { this.postFnArr.push(fn) } @@ -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 } }