Skip to content

Commit

Permalink
feat: add dynamic content filter (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
festoney8 committed Oct 30, 2024
1 parent 88bc97f commit 589d77f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 4.0.5

- 新增:动态内容关键词过滤

## 4.0.4

- 移除:稍后再看列表页 双列模式 适配页面变化
Expand Down
3 changes: 2 additions & 1 deletion src/components/items/EditorComp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ const saveSuccess = ref(false)
const editorData = ref('')

const updateData = () => {
editorData.value = BiliCleanerStorage.get<string[]>(item.id, []).join('\n') + '\n' // 末尾空行
const val = BiliCleanerStorage.get<string[]>(item.id, []).join('\n')
editorData.value = val ? val + '\n' : val // 末尾空行
}

const saveData = () => {
Expand Down
63 changes: 61 additions & 2 deletions src/modules/filters/variety/dynamic/pages/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { isPageDynamic } from '../../../../../utils/pageType'
import { BiliCleanerStorage } from '../../../../../utils/storage'
import { convertTimeToSec, orderedUniq, showEle, waitForEle } from '../../../../../utils/tool'
import { coreCheck } from '../../../core/core'
import { DynDurationFilter, DynUploaderFilter, DynVideoTitleFilter } from '../subFilters/black'
import { DynContentFilter, DynDurationFilter, DynUploaderFilter, DynVideoTitleFilter } from '../subFilters/black'

const GM_KEYS = {
black: {
Expand All @@ -28,6 +28,10 @@ const GM_KEYS = {
statusKey: 'dyn-title-keyword-filter-status',
valueKey: 'global-title-keyword-filter-value',
},
content: {
statusKey: 'dyn-content-keyword-filter-status',
valueKey: 'global-content-keyword-filter-value',
},
},
}

Expand All @@ -47,6 +51,16 @@ const selectorFns = {
title: (dyn: HTMLElement): SelectorResult => {
return dyn.querySelector('.bili-dyn-card-video__title')?.textContent?.trim()
},
content: (dyn: HTMLElement): SelectorResult => {
return Array.from(
dyn.querySelectorAll(
'.bili-dyn-content :is(.dyn-card-opus__title, .bili-rich-text__content > span:not(.bili-rich-text-module.at))',
),
)
.map((v) => v?.textContent?.trim())
.filter((v) => v?.trim())
.join(' ')
},
}

class DynamicFilterDynamic implements IMainFilter {
Expand All @@ -56,12 +70,14 @@ class DynamicFilterDynamic implements IMainFilter {
dynUploaderFilter = new DynUploaderFilter()
dynDurationFilter = new DynDurationFilter()
dynVideoTitleFilter = new DynVideoTitleFilter()
dynContentFilter = new DynContentFilter()

init() {
// 黑名单
this.dynUploaderFilter.setParam(BiliCleanerStorage.get(GM_KEYS.black.uploader.valueKey, []))
this.dynDurationFilter.setParam(BiliCleanerStorage.get(GM_KEYS.black.duration.valueKey, 0))
this.dynVideoTitleFilter.setParam(BiliCleanerStorage.get(GM_KEYS.black.title.valueKey, []))
this.dynContentFilter.setParam(BiliCleanerStorage.get(GM_KEYS.black.content.valueKey, []))
}

async check(mode?: 'full' | 'incr') {
Expand All @@ -70,7 +86,12 @@ class DynamicFilterDynamic implements IMainFilter {
}
let revertAll = false
if (
!(this.dynUploaderFilter.isEnable || this.dynDurationFilter.isEnable || this.dynVideoTitleFilter.isEnable)
!(
this.dynUploaderFilter.isEnable ||
this.dynDurationFilter.isEnable ||
this.dynVideoTitleFilter.isEnable ||
this.dynContentFilter.isEnable
)
) {
revertAll = true
}
Expand Down Expand Up @@ -101,6 +122,7 @@ class DynamicFilterDynamic implements IMainFilter {
`uploader: ${selectorFns.uploader(v)}`,
`title: ${selectorFns.title(v)}`,
`duration: ${selectorFns.duration(v)}`,
`content: ${selectorFns.content(v)}`,
].join('\n'),
)
})
Expand All @@ -111,6 +133,7 @@ class DynamicFilterDynamic implements IMainFilter {
this.dynUploaderFilter.isEnable && blackPairs.push([this.dynUploaderFilter, selectorFns.uploader])
this.dynDurationFilter.isEnable && blackPairs.push([this.dynDurationFilter, selectorFns.duration])
this.dynVideoTitleFilter.isEnable && blackPairs.push([this.dynVideoTitleFilter, selectorFns.title])
this.dynContentFilter.isEnable && blackPairs.push([this.dynContentFilter, selectorFns.content])

// 检测
const blackCnt = await coreCheck(dyns, true, blackPairs, [])
Expand Down Expand Up @@ -267,6 +290,42 @@ export const dynamicFilterDynamicGroups: Group[] = [
},
],
},
{
name: '动态内容过滤',
items: [
{
type: 'switch',
id: GM_KEYS.black.content.statusKey,
name: '启用 动态内容关键词过滤',
description: ['包含被转发动态内容', '不含动态内视频信息'],
defaultEnable: false,
noStyle: true,
enableFn: () => {
mainFilter.dynContentFilter.enable()
mainFilter.checkFull()
},
disableFn: () => {
mainFilter.dynContentFilter.disable()
mainFilter.checkFull()
},
},
{
type: 'editor',
id: GM_KEYS.black.content.valueKey,
name: '编辑 动态内容关键词黑名单',
editorTitle: '动态内容关键词 黑名单',
editorDescription: [
'每行一个关键词或正则,不区分大小写',
'请勿使用过于激进的关键词或正则',
'正则默认 iu 模式,无需 flag,语法:/abc|\\d+/',
],
saveFn: async () => {
mainFilter.dynContentFilter.setParam(BiliCleanerStorage.get(GM_KEYS.black.content.valueKey, []))
mainFilter.checkFull()
},
},
],
},
]

// 右键菜单handler
Expand Down
2 changes: 2 additions & 0 deletions src/modules/filters/variety/dynamic/subFilters/black.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export class DynUploaderFilter extends StringFilter {}
export class DynDurationFilter extends NumberMinFilter {}

export class DynVideoTitleFilter extends KeywordFilter {}

export class DynContentFilter extends KeywordFilter {}
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineConfig({
userscript: {
name: 'bilibili 页面净化大师',
namespace: 'http://tampermonkey.net/',
version: '4.0.4',
version: '4.0.5',
description:
'净化 B站/哔哩哔哩 页面,支持「精简功能、播放器净化、过滤视频、过滤评论、全站黑白名单」,提供 300+ 功能,定制自己的 B 站',
author: 'festoney8',
Expand Down

0 comments on commit 589d77f

Please sign in to comment.