Skip to content

Commit

Permalink
feat: update folder name, filter support temporary selector
Browse files Browse the repository at this point in the history
  • Loading branch information
festoney8 committed Jan 18, 2024
1 parent 56520a8 commit 4cc31e5
Show file tree
Hide file tree
Showing 18 changed files with 351 additions and 60 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
121 changes: 84 additions & 37 deletions src/filters/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export class MainFilter {
* @param selectors
*/
setupSelectors(selectors: Selectors) {
// 设定默认selector
this.durationSelector = selectors.duration ? selectors.duration : undefined
this.titleSelector = selectors.title ? selectors.title : undefined
this.uploaderSelector = selectors.uploader ? selectors.uploader : undefined
Expand Down Expand Up @@ -210,48 +211,94 @@ export class MainFilter {
* 支持首页、播放页右栏、热门视频/每周必看/排行榜
* @param videos 由调用函数传入的视频列表,包含一组视频HTML节点,每个节点内应包含 标题、时长、UP主、视频链接等
* @param sign 是否标记已过滤项
* @param selectors 可选, 用于覆盖默认selector, 用于例外情况
*/
checkAll(videos: HTMLElement[], sign = true) {
const checkDuration = this.durationEnable && this.durationFilterInstance && this.durationSelector
const checkTitle = this.titleEnable && this.titleFilterInstance && this.titleSelector
const checkUploader = this.uploaderEnable && this.uploaderFilterInstance && this.uploaderSelector
if (!checkDuration && !checkTitle && !checkUploader) {
return
}
videos.forEach((video) => {
// 构建任务,对标题、时长、UP主进行并行检查
const tasks: Promise<void>[] = []
if (checkDuration) {
const duration = video.querySelector(this.durationSelector!)?.textContent?.trim()
if (duration) {
tasks.push(this.durationFilterInstance!.check(duration))
}
checkAll(videos: HTMLElement[], sign = true, selectors?: Selectors) {
if (!selectors) {
// 使用默认selector
const checkDuration = this.durationEnable && this.durationFilterInstance && this.durationSelector
const checkTitle = this.titleEnable && this.titleFilterInstance && this.titleSelector
const checkUploader = this.uploaderEnable && this.uploaderFilterInstance && this.uploaderSelector
if (!checkDuration && !checkTitle && !checkUploader) {
return
}
if (checkTitle) {
const title = video.querySelector(this.titleSelector!)?.textContent?.trim()
if (title) {
tasks.push(this.titleFilterInstance!.check(title))
videos.forEach((video) => {
// 构建任务,对标题、时长、UP主进行并行检查
const tasks: Promise<void>[] = []
if (checkDuration) {
const duration = video.querySelector(this.durationSelector!)?.textContent?.trim()
if (duration) {
tasks.push(this.durationFilterInstance!.check(duration))
}
}
}
if (checkUploader) {
const uploader = video.querySelector(this.uploaderSelector!)?.textContent?.trim()
if (uploader) {
tasks.push(this.uploaderFilterInstance!.check(uploader))
if (checkTitle) {
const title = video.querySelector(this.titleSelector!)?.textContent?.trim()
if (title) {
tasks.push(this.titleFilterInstance!.check(title))
}
}
if (checkUploader) {
const uploader = video.querySelector(this.uploaderSelector!)?.textContent?.trim()
if (uploader) {
tasks.push(this.uploaderFilterInstance!.check(uploader))
}
}
Promise.all(tasks)
.then(() => {
this.showVideo(video)
})
.catch(() => {
this.hideVideo(video)
})
.finally(() => {
// 标记已过滤的视频
if (sign) {
video.setAttribute(settings.filterSign, '')
}
})
})
} else {
// 使用临时selector
const checkDuration = this.durationEnable && this.durationFilterInstance && selectors.duration
const checkTitle = this.titleEnable && this.titleFilterInstance && selectors.title
const checkUploader = this.uploaderEnable && this.uploaderFilterInstance && selectors.uploader
if (!checkDuration && !checkTitle && !checkUploader) {
return
}
Promise.all(tasks)
.then(() => {
this.showVideo(video)
})
.catch(() => {
this.hideVideo(video)
})
.finally(() => {
// 标记已过滤的视频
if (sign) {
video.setAttribute(settings.filterSign, '')
videos.forEach((video) => {
// 构建任务,对标题、时长、UP主进行并行检查
const tasks: Promise<void>[] = []
if (checkDuration) {
const duration = video.querySelector(selectors.duration!)?.textContent?.trim()
if (duration) {
tasks.push(this.durationFilterInstance!.check(duration))
}
})
})
}
if (checkTitle) {
const title = video.querySelector(selectors.title!)?.textContent?.trim()
if (title) {
tasks.push(this.titleFilterInstance!.check(title))
}
}
if (checkUploader) {
const uploader = video.querySelector(selectors.uploader!)?.textContent?.trim()
if (uploader) {
tasks.push(this.uploaderFilterInstance!.check(uploader))
}
}
Promise.all(tasks)
.then(() => {
this.showVideo(video)
})
.catch(() => {
this.hideVideo(video)
})
.finally(() => {
if (sign) {
video.setAttribute(settings.filterSign, '')
}
})
})
}
}
}
6 changes: 3 additions & 3 deletions src/filters/homepage-filter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GM_getValue } from '$'
import { Group } from '../core/group'
import { CheckboxItem, NumberItem } from '../core/item'
import { Group } from '../components/group'
import { CheckboxItem, NumberItem } from '../components/item'
import { isPageHomepage } from '../utils/page-type'
import { debug, error } from '../utils/logger'
import { DurationFilterConfig, MainFilter } from './filters'
Expand All @@ -16,7 +16,7 @@ const homepageDurationFilterValueID = 'homepage-duration-filter-threshold'

// 主过滤器配置
const mainFilterInstanse = new MainFilter()
// 设定selector
// 设定默认selector
mainFilterInstanse.setupSelectors({
duration: 'span.bili-video-card__stats__duration',
title: 'h3.bili-video-card__info--tit',
Expand Down
Loading

0 comments on commit 4cc31e5

Please sign in to comment.