Skip to content

Commit

Permalink
feat: complete contextmenu
Browse files Browse the repository at this point in the history
  • Loading branch information
festoney8 committed Mar 19, 2024
1 parent 2829fa3 commit 88693f6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 89 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 3.2.0

- 新增:评论区过滤器(用户过滤、评论关键词过滤)
- 新增:评论区过滤支持播放页、番剧页
- 优化:URL净化功能

## 3.1.11
Expand Down
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

</div>

## 主要功能:页面净化、视频过滤
## 主要功能:页面净化、视频过滤、评论过滤

- **页面净化**:隐藏网页内各种元素,去除无用功能,净化顶栏、播放器、评论区等

- **视频过滤**:根据视频时长、UP 主黑名单、标题关键词、BV 号筛选视频推荐

- **评论过滤**:根据用户名、评论内容关键词筛选评论

![](images/preview.jpg)

## 安装
Expand All @@ -36,6 +38,7 @@
>
> - **页面净化:「首页、播放页、影视番剧播放页、直播间、搜索页、动态页、热门页、频道页」**
> - **视频过滤:「首页、播放页、搜索页、热门页、频道页」**
> - **评论过滤:「播放页、影视番剧播放页」**
> - **每个页面都有独立菜单**,按下图打开菜单进行设置,实时生效
![](images/usage.png)
Expand Down Expand Up @@ -80,7 +83,19 @@
> - 如果你在意推送质量,不要点开不喜欢的视频,不要点开不喜欢的 UP 主的个人页
> - 点开视频/点开个人页是正反馈,推荐系统误认为你喜欢,导致推送变差,请复制链接使用无痕浏览
### 3. 快捷键
### 3. 评论过滤

- 评论过滤支持「用户名、关键词」两种模式
- 关键词黑名单大小写不敏感。支持正则(大小写不敏感),语法:`/abc|\d+/`
- **白名单权限高于黑名单,命中白名单的评论不会被隐藏**

> [!NOTE]
>
> - **关键词不宜过于简单**,会误伤很多评论
> - **编写正则要慎重**,可能造成大量评论屏蔽和频繁载入
> - **屏蔽后,如果评论没有消失,很可能命中了白名单**
### 4. 快捷键

- 支持使用快捷键 `Alt + B``Ctrl + Alt + B` 快速开启/关闭**页面净化**功能,迅速查看净化对比
- Firefox 浏览器仅支持 `Ctrl + Alt + B`
Expand Down
138 changes: 61 additions & 77 deletions src/filters/commentFilter/pages/video.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { GM_getValue } from '$'
import { Group } from '../../../components/group'
import { CheckboxItem, ButtonItem } from '../../../components/item'
import settings from '../../../settings'
import { debugCommentFilter as debug, error } from '../../../utils/logger'
import { isPageBangumi, isPageVideo } from '../../../utils/page-type'
import { showEle, waitForEle } from '../../../utils/tool'
import coreCommentFilterInstance, { CommentSelectorFunc } from '../filters/core'
import { ContentAction, UsernameAction } from './actions/action'
import contextMenuInstance from '../../../components/contextmenu'
import coreCommentFilterInstance, { CommentSelectorFunc } from '../filters/core'
import settings from '../../../settings'

const videoPageCommentFilterGroupList: Group[] = []

// 右键菜单功能
// let isContextMenuFuncRunning = false
// let isContextMenuUsernameEnable = false
let isContextMenuFuncRunning = false
let isContextMenuUsernameEnable = false

// 白名单功能开关
let isRootCommentWhitelistEnable: boolean = GM_getValue('BILICLEANER_video-comment-root-whitelist-status', false)
Expand Down Expand Up @@ -85,7 +86,6 @@ if (isPageVideo() || isPageBangumi()) {
`.sub-reply-item:not(.jump-link.user):not([${settings.filterSign}])`,
)
}
debug(rootComments.length, subComments.length)

// 白名单过滤
rootComments = Array.from(rootComments).filter((e) => {
Expand All @@ -96,7 +96,9 @@ if (isPageVideo() || isPageBangumi()) {
(isPinnedCommentWhitelistEnable && e.querySelector('.root-reply-container .top-icon')) ||
(isLinkCommentWhitelistEnable &&
e.querySelector(
'.root-reply-container .jump-link.video-time, .root-reply-container .jump-link.normal, .root-reply-container .jump-link.video',
`.root-reply-container .jump-link.video-time,
.root-reply-container .jump-link.normal,
.root-reply-container .jump-link.video`,
))
if (isWhite) {
showEle(e)
Expand All @@ -109,7 +111,9 @@ if (isPageVideo() || isPageBangumi()) {
(isUploaderCommentWhitelistEnable && e.querySelector('.sub-up-icon')) ||
(isLinkCommentWhitelistEnable &&
e.querySelector(
'.root-reply-container .jump-link.video-time, .root-reply-container .jump-link.normal, .root-reply-container .jump-link.video',
`.jump-link.video-time,
.jump-link.normal,
.jump-link.video`,
))

if (isWhite) {
Expand Down Expand Up @@ -167,61 +171,39 @@ if (isPageVideo() || isPageBangumi()) {
)
//=======================================================================================

// // 右键监听函数, 右键单击指定元素时修改右键菜单, 用于屏蔽评论用户
// const contextMenuFunc = () => {
// if (isContextMenuFuncRunning) {
// return
// }
// isContextMenuFuncRunning = true
// // 监听右键单击
// document.addEventListener('contextmenu', (e) => {
// if (e.target instanceof HTMLElement) {
// // debug(e.target.classList)
// const target = e.target
// if (
// isContextMenuUsernameEnable &&
// target.classList.contains('name')
// // target.closest('.upname span.name') === target
// ) {
// // 命中UP主
// const uploader = target.textContent
// if (uploader) {
// e.preventDefault()
// const onclickBlack = () => {
// usernameAction.add(uploader)
// }
// contextMenuInstance.registerMenu(`◎ 屏蔽UP主:${uploader}`, onclickBlack)
// contextMenuInstance.show(e.clientX, e.clientY)
// }
// } else if (
// isContextMenuBvidEnable &&
// target.classList.contains('title')
// // target.closest('.info > a > p') === target
// ) {
// // 命中评论标题, 提取bvid
// const href = target.parentElement?.getAttribute('href')
// if (href) {
// const bvid = matchBvid(href)
// if (bvid) {
// e.preventDefault()
// const onclick = () => {
// commentBvidAction.add(bvid)
// }
// contextMenuInstance.registerMenu(`屏蔽评论:${bvid}`, onclick)
// contextMenuInstance.show(e.clientX, e.clientY)
// }
// }
// } else {
// contextMenuInstance.hide()
// }
// }
// })
// // 监听左键单击,关闭右键菜单
// document.addEventListener('click', () => {
// contextMenuInstance.hide()
// })
// debug('contextMenuFunc listen contextmenu')
// }
// 右键监听函数, 屏蔽评论用户
const contextMenuFunc = () => {
if (isContextMenuFuncRunning) {
return
}
isContextMenuFuncRunning = true
document.addEventListener('contextmenu', (e) => {
if (e.target instanceof HTMLElement) {
const target = e.target
if (
isContextMenuUsernameEnable &&
(target.classList.contains('user-name') || target.classList.contains('sub-user-name'))
) {
// 命中用户
const username = target.textContent?.trim()
if (username) {
e.preventDefault()
contextMenuInstance.registerMenu(`◎ 屏蔽用户:${username}`, () => {
usernameAction.add(username)
})
contextMenuInstance.show(e.clientX, e.clientY)
}
} else {
contextMenuInstance.hide()
}
}
})
// 关闭右键菜单
document.addEventListener('click', () => {
contextMenuInstance.hide()
})
debug('contextMenuFunc listen contextmenu')
}

//=======================================================================================
// 构建UI菜单
Expand All @@ -231,16 +213,16 @@ if (isPageVideo() || isPageBangumi()) {
// 启用 播放页UP主过滤
new CheckboxItem({
itemID: usernameAction.statusKey,
description: '启用 评论区 用户名过滤',
description: '启用 评论区 用户名过滤\n(右键单击用户名)',
itemFunc: () => {
// 启用右键菜单功能
// isContextMenuUsernameEnable = true
// contextMenuFunc()
isContextMenuUsernameEnable = true
contextMenuFunc()
usernameAction.enable()
},
callback: () => {
// 禁用右键菜单功能
// isContextMenuUsernameEnable = false
isContextMenuUsernameEnable = false
usernameAction.disable()
},
}),
Expand All @@ -255,7 +237,7 @@ if (isPageVideo() || isPageBangumi()) {
}),
]
videoPageCommentFilterGroupList.push(
new Group('comment-username-filter-group', '播放页 评论用户过滤 (右键单击用户名)', usernameItems),
new Group('comment-username-filter-group', '播放页 评论区 用户过滤', usernameItems),
)

// UI组件, 评论内容过滤part
Expand All @@ -281,9 +263,7 @@ if (isPageVideo() || isPageBangumi()) {
},
}),
]
videoPageCommentFilterGroupList.push(
new Group('comment-content-filter-group', '播放页 评论关键词过滤', contentItems),
)
videoPageCommentFilterGroupList.push(new Group('comment-content-filter-group', '评论区 关键词过滤', contentItems))

// UI组件, 白名单part
const whitelistItems = [
Expand Down Expand Up @@ -313,10 +293,11 @@ if (isPageVideo() || isPageBangumi()) {
checkCommentList(true)
},
}),
// UP主的评论 免过滤
// UP主的评论 免过滤, 默认开启
new CheckboxItem({
itemID: 'video-comment-uploader-whitelist-status',
description: 'UP主的评论 免过滤',
defaultStatus: true,
itemFunc: () => {
isUploaderCommentWhitelistEnable = true
checkCommentList(true)
Expand All @@ -326,10 +307,11 @@ if (isPageVideo() || isPageBangumi()) {
checkCommentList(true)
},
}),
// 置顶评论 免过滤
// 置顶评论 免过滤, 默认开启
new CheckboxItem({
itemID: 'video-comment-pinned-whitelist-status',
description: '置顶评论 免过滤',
defaultStatus: true,
itemFunc: () => {
isPinnedCommentWhitelistEnable = true
checkCommentList(true)
Expand All @@ -339,10 +321,11 @@ if (isPageVideo() || isPageBangumi()) {
checkCommentList(true)
},
}),
// 笔记评论 免过滤
// 笔记评论 免过滤, 默认开启
new CheckboxItem({
itemID: 'video-comment-note-whitelist-status',
description: '笔记评论/图片评论 免过滤',
description: '笔记/图片评论 免过滤',
defaultStatus: true,
itemFunc: () => {
isNoteCommentWhitelistEnable = true
checkCommentList(true)
Expand All @@ -352,10 +335,11 @@ if (isPageVideo() || isPageBangumi()) {
checkCommentList(true)
},
}),
// 含超链接的评论 免过滤
// 含超链接的评论 免过滤, 默认开启
new CheckboxItem({
itemID: 'video-comment-link-whitelist-status',
description: '含超链接的评论 免过滤',
description: '含超链接的评论 免过滤\n(站内视频/URL/播放时间跳转)',
defaultStatus: true,
itemFunc: () => {
isLinkCommentWhitelistEnable = true
checkCommentList(true)
Expand All @@ -367,7 +351,7 @@ if (isPageVideo() || isPageBangumi()) {
}),
]
videoPageCommentFilterGroupList.push(
new Group('comment-content-filter-whitelist-group', '白名单设置 (免过滤)', whitelistItems),
new Group('comment-content-filter-whitelist-group', '评论区 白名单设置 (免过滤)', whitelistItems),
)
}

Expand Down
25 changes: 15 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,30 @@ const main = async () => {
debug(`${mode} panel replace complete`)
}
}
// 页面净化设置
GM_registerMenuCommand('✅页面净化设置', () => {
createPanelWithMode('rule', RULE_GROUPS)
})
// 视频过滤设置
if (isPageHomepage() || isPageVideo() || isPagePopular() || isPageSearch() || isPageChannel()) {
GM_registerMenuCommand('✅视频过滤设置', () => {
createPanelWithMode('filter', VIDEO_FILTER_GROUPS)
createPanelWithMode('videoFilter', VIDEO_FILTER_GROUPS)
})

// 过滤器快捷按钮
}
// 评论过滤设置
if (isPageVideo() || isPageBangumi()) {
GM_registerMenuCommand('✅评论过滤设置', () => {
createPanelWithMode('commentFilter', COMMENT_FILTER_GROUPS)
})
}
// 视频过滤 快捷按钮
if (isPageHomepage() || isPageVideo() || isPagePopular() || isPageSearch() || isPageChannel()) {
const videoFilterSideBtnID = 'video-filter-side-btn'
const sideBtn = new SideBtn(
videoFilterSideBtnID,
'视频过滤',
() => {
createPanelWithMode('filter', VIDEO_FILTER_GROUPS)
createPanelWithMode('videoFilter', VIDEO_FILTER_GROUPS)
},
() => {
panelInstance.hide()
Expand All @@ -164,18 +173,14 @@ const main = async () => {
})
}
}
// 评论过滤 快捷按钮
if (isPageVideo() || isPageBangumi()) {
GM_registerMenuCommand('✅评论过滤设置', () => {
createPanelWithMode('filter', COMMENT_FILTER_GROUPS)
})

// 评论过滤 快捷按钮
const commentFilterSideBtnID = 'comment-filter-side-btn'
const sideBtn = new SideBtn(
commentFilterSideBtnID,
'评论过滤',
() => {
createPanelWithMode('filter', COMMENT_FILTER_GROUPS)
createPanelWithMode('commentFilter', COMMENT_FILTER_GROUPS)
},
() => {
panelInstance.hide()
Expand Down

0 comments on commit 88693f6

Please sign in to comment.