Skip to content

Commit

Permalink
chore: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
festoney8 committed Jun 22, 2024
1 parent 5488e96 commit 37c6ac1
Show file tree
Hide file tree
Showing 15 changed files with 509 additions and 538 deletions.
60 changes: 36 additions & 24 deletions src/components/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ interface ICheckboxItemOption {
itemID: string
description: string
defaultStatus?: boolean
itemFunc?: () => void
enableFunc?: () => Promise<void>
// 分别对应:立即执行,DOMContentLoaded, load
enableFuncRunAt?: 'document-start' | 'document-end' | 'document-idle'
disableFunc?: () => Promise<void>
itemCSS?: string
callback?: () => void
}

/** 普通开关 */
Expand Down Expand Up @@ -123,16 +125,11 @@ export class CheckboxItem implements IItem {
if ((<HTMLInputElement>event.target).checked) {
this.setStatus(true)
this.insertItemCSS()
if (this.option.itemFunc !== undefined) {
this.option.itemFunc()
}
this.option.enableFunc && this.option.enableFunc().then().catch()
} else {
this.setStatus(false)
this.removeItemCSS()
// 回调
if (typeof this.option.callback === 'function') {
this.option.callback()
}
this.option.disableFunc && this.option.disableFunc().then().catch()
}
})
debug(`watchItem ${this.option.itemID} OK`)
Expand All @@ -150,8 +147,28 @@ export class CheckboxItem implements IItem {
if (this.isEnable) {
try {
this.insertItemCSS()
if (enableFunc && this.option.itemFunc instanceof Function) {
this.option.itemFunc()
if (enableFunc && this.option.enableFunc) {
switch (this.option.enableFuncRunAt) {
case 'document-start':
this.option.enableFunc().then().catch()
break
case 'document-end':
if (['complete', 'interactive'].includes(document.readyState)) {
this.option.enableFunc().then().catch()
} else {
document.addEventListener('DOMContentLoaded', this.option.enableFunc)
}
break
case 'document-idle':
if (document.readyState === 'complete') {
this.option.enableFunc().then().catch()
} else {
document.addEventListener('load', this.option.enableFunc)
}
break
default:
this.option.enableFunc().then().catch()
}
}
debug(`enableItem ${this.option.itemID} OK`)
} catch (err) {
Expand All @@ -177,7 +194,7 @@ interface IRadioItemOption {
radioName: string
radioItemIDList: string[]
defaultStatus?: boolean
itemFunc?: () => void
itemFunc?: () => Promise<void>
itemCSS?: string
}

Expand Down Expand Up @@ -282,9 +299,7 @@ export class RadioItem implements IItem {
debug(`radioItem ${this.option.itemID} checked`)
this.setStatus(true)
this.insertItemCSS()
if (this.option.itemFunc !== undefined) {
this.option.itemFunc()
}
this.option.itemFunc && this.option.itemFunc().then().catch()
// 相同name的其他option自动置为uncheck, 但这一行为无法被监听, 需传入itemID逐一修改
this.option.radioItemIDList.forEach((targetID) => {
if (targetID !== this.option.itemID) {
Expand Down Expand Up @@ -317,8 +332,8 @@ export class RadioItem implements IItem {
if (this.isEnable) {
try {
this.insertItemCSS()
if (enableFunc && this.option.itemFunc instanceof Function) {
this.option.itemFunc()
if (enableFunc && this.option.itemFunc) {
this.option.itemFunc().then().catch()
}
debug(`enableItem ${this.option.itemID} OK`)
} catch (err) {
Expand Down Expand Up @@ -352,7 +367,7 @@ interface INumberItemOption {
itemCSS?: string
// CSS中待替换为数值的占位符
itemCSSPlaceholder?: string
callback?: (value: number) => void
callback?: (value: number) => Promise<void>
}

/** 数值设定 */
Expand Down Expand Up @@ -466,10 +481,7 @@ export class NumberItem implements IItem {
debug(`${this.option.itemID} currValue ${itemEle.value}`)
// reload
this.reloadItem()
// 调用回调函数
if (this.option.callback && typeof this.option.callback === 'function') {
this.option.callback(parseInt(itemEle.value))
}
this.option.callback && this.option.callback(parseInt(itemEle.value)).then().catch()
})
debug(`watchItem ${this.option.itemID} OK`)
} catch (err) {
Expand Down Expand Up @@ -517,7 +529,7 @@ interface IButtonItemOption {
itemID: string
description: string
name: string
itemFunc: () => void
itemFunc: () => Promise<void>
}

/** 普通按钮 */
Expand Down Expand Up @@ -552,7 +564,7 @@ export class ButtonItem implements IItem {
const itemEle = document.querySelector(`#${this.option.itemID} button`) as HTMLButtonElement
itemEle.addEventListener('click', () => {
debug(`button ${this.option.itemID} click`)
this.option.itemFunc()
this.option.itemFunc().then().catch()
})
debug(`watchItem ${this.option.itemID} OK`)
} catch (err) {
Expand Down
36 changes: 18 additions & 18 deletions src/filters/commentFilter/pages/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,13 @@ if (isPageDynamic()) {
new CheckboxItem({
itemID: usernameAction.statusKey,
description: '启用 评论区 用户名过滤\n(右键单击用户名)',
itemFunc: () => {
enableFunc: async () => {
// 启用右键菜单功能
isContextMenuUsernameEnable = true
contextMenuFunc()
usernameAction.enable()
},
callback: () => {
disableFunc: async () => {
// 禁用右键菜单功能
isContextMenuUsernameEnable = false
usernameAction.disable()
Expand All @@ -246,7 +246,7 @@ if (isPageDynamic()) {
itemID: 'comment-username-edit-button',
description: '编辑 用户名黑名单',
name: '编辑',
itemFunc: () => {
itemFunc: async () => {
usernameAction.blacklist.show()
},
}),
Expand All @@ -261,10 +261,10 @@ if (isPageDynamic()) {
new CheckboxItem({
itemID: contentAction.statusKey,
description: '启用 评论区 关键词过滤',
itemFunc: () => {
enableFunc: async () => {
contentAction.enable()
},
callback: () => {
disableFunc: async () => {
contentAction.disable()
},
}),
Expand All @@ -273,7 +273,7 @@ if (isPageDynamic()) {
itemID: 'comment-content-edit-button',
description: '编辑 评论关键词黑名单(支持正则)',
name: '编辑',
itemFunc: () => {
itemFunc: async () => {
contentAction.blacklist.show()
},
}),
Expand All @@ -286,11 +286,11 @@ if (isPageDynamic()) {
new CheckboxItem({
itemID: 'dynamic-comment-root-whitelist-status',
description: '一级评论(主评论) 免过滤',
itemFunc: () => {
enableFunc: async () => {
isRootCommentWhitelistEnable = true
checkCommentList(true)
},
callback: () => {
disableFunc: async () => {
isRootCommentWhitelistEnable = false
checkCommentList(true)
},
Expand All @@ -299,11 +299,11 @@ if (isPageDynamic()) {
new CheckboxItem({
itemID: 'dynamic-comment-sub-whitelist-status',
description: '二级评论(回复) 免过滤',
itemFunc: () => {
enableFunc: async () => {
isSubCommentWhitelistEnable = true
checkCommentList(true)
},
callback: () => {
disableFunc: async () => {
isSubCommentWhitelistEnable = false
checkCommentList(true)
},
Expand All @@ -313,11 +313,11 @@ if (isPageDynamic()) {
itemID: 'dynamic-comment-uploader-whitelist-status',
description: 'UP主的评论 免过滤',
defaultStatus: true,
itemFunc: () => {
enableFunc: async () => {
isUploaderCommentWhitelistEnable = true
checkCommentList(true)
},
callback: () => {
disableFunc: async () => {
isUploaderCommentWhitelistEnable = false
checkCommentList(true)
},
Expand All @@ -327,11 +327,11 @@ if (isPageDynamic()) {
itemID: 'dynamic-comment-pinned-whitelist-status',
description: '置顶评论 免过滤',
defaultStatus: true,
itemFunc: () => {
enableFunc: async () => {
isPinnedCommentWhitelistEnable = true
checkCommentList(true)
},
callback: () => {
disableFunc: async () => {
isPinnedCommentWhitelistEnable = false
checkCommentList(true)
},
Expand All @@ -341,11 +341,11 @@ if (isPageDynamic()) {
itemID: 'dynamic-comment-note-whitelist-status',
description: '笔记/图片评论 免过滤',
defaultStatus: true,
itemFunc: () => {
enableFunc: async () => {
isNoteCommentWhitelistEnable = true
checkCommentList(true)
},
callback: () => {
disableFunc: async () => {
isNoteCommentWhitelistEnable = false
checkCommentList(true)
},
Expand All @@ -355,11 +355,11 @@ if (isPageDynamic()) {
itemID: 'dynamic-comment-link-whitelist-status',
description: '含超链接的评论 免过滤\n(站内视频/URL/播放时间跳转)',
defaultStatus: true,
itemFunc: () => {
enableFunc: async () => {
isLinkCommentWhitelistEnable = true
checkCommentList(true)
},
callback: () => {
disableFunc: async () => {
isLinkCommentWhitelistEnable = false
checkCommentList(true)
},
Expand Down
36 changes: 18 additions & 18 deletions src/filters/commentFilter/pages/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ if (isPageVideo() || isPageBangumi() || isPagePlaylist()) {
new CheckboxItem({
itemID: usernameAction.statusKey,
description: '启用 评论区 用户名过滤\n(右键单击用户名)',
itemFunc: () => {
enableFunc: async () => {
// 启用右键菜单功能
isContextMenuUsernameEnable = true
contextMenuFunc()
usernameAction.enable()
},
callback: () => {
disableFunc: async () => {
// 禁用右键菜单功能
isContextMenuUsernameEnable = false
usernameAction.disable()
Expand All @@ -232,7 +232,7 @@ if (isPageVideo() || isPageBangumi() || isPagePlaylist()) {
itemID: 'comment-username-edit-button',
description: '编辑 用户名黑名单',
name: '编辑',
itemFunc: () => {
itemFunc: async () => {
usernameAction.blacklist.show()
},
}),
Expand All @@ -247,10 +247,10 @@ if (isPageVideo() || isPageBangumi() || isPagePlaylist()) {
new CheckboxItem({
itemID: contentAction.statusKey,
description: '启用 评论区 关键词过滤',
itemFunc: () => {
enableFunc: async () => {
contentAction.enable()
},
callback: () => {
disableFunc: async () => {
contentAction.disable()
},
}),
Expand All @@ -259,7 +259,7 @@ if (isPageVideo() || isPageBangumi() || isPagePlaylist()) {
itemID: 'comment-content-edit-button',
description: '编辑 评论关键词黑名单(支持正则)',
name: '编辑',
itemFunc: () => {
itemFunc: async () => {
contentAction.blacklist.show()
},
}),
Expand All @@ -272,11 +272,11 @@ if (isPageVideo() || isPageBangumi() || isPagePlaylist()) {
new CheckboxItem({
itemID: 'video-comment-root-whitelist-status',
description: '一级评论(主评论) 免过滤',
itemFunc: () => {
enableFunc: async () => {
isRootCommentWhitelistEnable = true
checkCommentList(true)
},
callback: () => {
disableFunc: async () => {
isRootCommentWhitelistEnable = false
checkCommentList(true)
},
Expand All @@ -285,11 +285,11 @@ if (isPageVideo() || isPageBangumi() || isPagePlaylist()) {
new CheckboxItem({
itemID: 'video-comment-sub-whitelist-status',
description: '二级评论(回复) 免过滤',
itemFunc: () => {
enableFunc: async () => {
isSubCommentWhitelistEnable = true
checkCommentList(true)
},
callback: () => {
disableFunc: async () => {
isSubCommentWhitelistEnable = false
checkCommentList(true)
},
Expand All @@ -299,11 +299,11 @@ if (isPageVideo() || isPageBangumi() || isPagePlaylist()) {
itemID: 'video-comment-uploader-whitelist-status',
description: 'UP主的评论 免过滤',
defaultStatus: true,
itemFunc: () => {
enableFunc: async () => {
isUploaderCommentWhitelistEnable = true
checkCommentList(true)
},
callback: () => {
disableFunc: async () => {
isUploaderCommentWhitelistEnable = false
checkCommentList(true)
},
Expand All @@ -313,11 +313,11 @@ if (isPageVideo() || isPageBangumi() || isPagePlaylist()) {
itemID: 'video-comment-pinned-whitelist-status',
description: '置顶评论 免过滤',
defaultStatus: true,
itemFunc: () => {
enableFunc: async () => {
isPinnedCommentWhitelistEnable = true
checkCommentList(true)
},
callback: () => {
disableFunc: async () => {
isPinnedCommentWhitelistEnable = false
checkCommentList(true)
},
Expand All @@ -327,11 +327,11 @@ if (isPageVideo() || isPageBangumi() || isPagePlaylist()) {
itemID: 'video-comment-note-whitelist-status',
description: '笔记/图片评论 免过滤',
defaultStatus: true,
itemFunc: () => {
enableFunc: async () => {
isNoteCommentWhitelistEnable = true
checkCommentList(true)
},
callback: () => {
disableFunc: async () => {
isNoteCommentWhitelistEnable = false
checkCommentList(true)
},
Expand All @@ -341,11 +341,11 @@ if (isPageVideo() || isPageBangumi() || isPagePlaylist()) {
itemID: 'video-comment-link-whitelist-status',
description: '含超链接的评论 免过滤\n(站内视频/URL/播放时间跳转)',
defaultStatus: true,
itemFunc: () => {
enableFunc: async () => {
isLinkCommentWhitelistEnable = true
checkCommentList(true)
},
callback: () => {
disableFunc: async () => {
isLinkCommentWhitelistEnable = false
checkCommentList(true)
},
Expand Down
Loading

0 comments on commit 37c6ac1

Please sign in to comment.