Skip to content

Commit

Permalink
update: util func
Browse files Browse the repository at this point in the history
  • Loading branch information
festoney8 committed Jun 16, 2024
1 parent 201e00d commit 2e82c96
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 46 deletions.
17 changes: 7 additions & 10 deletions src/filters/commentFilter/pages/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,13 @@ if (isPageDynamic()) {
waitForEle(
document,
'.bili-dyn-home--member, .bili-comment-container, .bili-comment, #app',
(node: Node): boolean => {
if (node instanceof HTMLElement) {
return (
(node as HTMLElement).className === 'bili-dyn-home--member' ||
(node as HTMLElement).className.includes('bili-comment-container') ||
(node as HTMLElement).className.includes('bili-comment') ||
(node as HTMLElement).id === 'app'
)
}
return false
(node: HTMLElement): boolean => {
return (
node.className === 'bili-dyn-home--member' ||
node.className?.includes('bili-comment-container') ||
node.className?.includes('bili-comment') ||
node.id === 'app'
)
},
).then((ele) => {
if (ele) {
Expand Down
8 changes: 2 additions & 6 deletions src/filters/commentFilter/pages/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,8 @@ if (isPageVideo() || isPageBangumi() || isPagePlaylist()) {
}

try {
waitForEle(document, '#comment, #comment-body, .playlist-comment', (node: Node): boolean => {
return (
node instanceof HTMLElement &&
(['comment', 'comment-body'].includes((node as HTMLElement).id) ||
(node as HTMLElement).className === 'playlist-comment')
)
waitForEle(document, '#comment, #comment-body, .playlist-comment', (node: HTMLElement): boolean => {
return ['comment', 'comment-body'].includes(node.id) || node.className === 'playlist-comment'
}).then((ele) => {
if (ele) {
commentListContainer = ele
Expand Down
4 changes: 2 additions & 2 deletions src/filters/videoFilter/pages/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ if (isPageChannel()) {
}
try {
// 监听视频列表出现
waitForEle(document, 'main.channel-layout', (node: Node): boolean => {
return node instanceof HTMLElement && (node as HTMLElement).className === 'channel-layout'
waitForEle(document, 'main.channel-layout', (node: HTMLElement): boolean => {
return node.className === 'channel-layout'
}).then((ele) => {
if (ele) {
videoListContainer = ele
Expand Down
4 changes: 2 additions & 2 deletions src/filters/videoFilter/pages/homepage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ if (isPageHomepage()) {

try {
// 监听视频列表出现
waitForEle(document, '.container.is-version8', (node: Node): boolean => {
return node instanceof HTMLElement && (node as HTMLElement).className === 'container is-version8'
waitForEle(document, '.container.is-version8', (node: HTMLElement): boolean => {
return node.className === 'container is-version8'
}).then((ele) => {
if (ele) {
videoListContainer = ele
Expand Down
4 changes: 2 additions & 2 deletions src/filters/videoFilter/pages/popular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ if (isPagePopular()) {

try {
// 监听视频列表出现
waitForEle(document, '#app', (node: Node): boolean => {
return node instanceof HTMLElement && (node as HTMLElement).id === 'app'
waitForEle(document, '#app', (node: HTMLElement): boolean => {
return node.id === 'app'
}).then((ele) => {
if (ele) {
videoListContainer = ele
Expand Down
4 changes: 2 additions & 2 deletions src/filters/videoFilter/pages/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ if (isPageSearch()) {

try {
// 监听视频列表出现
waitForEle(document, '.search-content', (node: Node): boolean => {
return node instanceof HTMLElement && (node as HTMLElement).className?.includes('search-content')
waitForEle(document, '.search-content', (node: HTMLElement): boolean => {
return node.className?.includes('search-content')
}).then((ele) => {
if (ele) {
videoListContainer = ele
Expand Down
4 changes: 2 additions & 2 deletions src/filters/videoFilter/pages/space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ if (isPageSpace()) {
}
try {
// 监听视频列表出现
waitForEle(document, '#app', (node: Node): boolean => {
return node instanceof HTMLElement && (node as HTMLElement).id === 'app'
waitForEle(document, '#app', (node: HTMLElement): boolean => {
return node.id === 'app'
}).then((ele) => {
if (ele) {
videoListContainer = ele
Expand Down
8 changes: 2 additions & 6 deletions src/filters/videoFilter/pages/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,8 @@ if (isPageVideo() || isPagePlaylist()) {

try {
// 监听视频列表出现
waitForEle(document, '#reco_list, .recommend-list-container', (node: Node): boolean => {
return (
node instanceof HTMLElement &&
((node as HTMLElement).id === 'reco_list' ||
(node as HTMLElement).className === 'recommend-list-container')
)
waitForEle(document, '#reco_list, .recommend-list-container', (node: HTMLElement): boolean => {
return node.id === 'reco_list' || node.className === 'recommend-list-container'
}).then((ele) => {
if (ele) {
videoListContainer = ele
Expand Down
2 changes: 1 addition & 1 deletion src/rules/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from '../utils/page-type'
import URLCleanerInstance from '../utils/url-cleaner'

// Grouplist
const commonGroupList: Group[] = []

// 通用 页面直角化,去除圆角,根据URL选取CSS
Expand Down Expand Up @@ -421,6 +420,7 @@ const basicItems = [
}),
]
commonGroupList.push(new Group('common-basic', '全站通用项 基本功能', basicItems))

// 通用header净化,直播首页除外
if (!isPageLiveHome()) {
// 顶栏左侧
Expand Down
19 changes: 8 additions & 11 deletions src/rules/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ if (isPageVideo() || isPagePlaylist()) {
partNum += `?p=${params.get('p')}`
}
const aid = dec(bvid)
return `https://www.bilibili.com/video/av${aid}/${partNum}${urlObj.hash}`
if (partNum || urlObj.hash) {
return `https://www.bilibili.com/video/av${aid}/${partNum}${urlObj.hash}`
}
return `https://www.bilibili.com/video/av${aid}`
}
}
return url
Expand Down Expand Up @@ -184,11 +187,8 @@ if (isPageVideo() || isPagePlaylist()) {
const listener = () => {
window.scrollTo(0, 60)
// 监听宽屏按钮出现
waitForEle(document.body, '.bpx-player-ctrl-wide', (node: Node): boolean => {
return (
node instanceof HTMLElement &&
(node as HTMLElement).className.includes('bpx-player-ctrl-wide')
)
waitForEle(document.body, '.bpx-player-ctrl-wide', (node: HTMLElement): boolean => {
return node.className?.includes('bpx-player-ctrl-wide')
}).then((wideBtn) => {
if (wideBtn) {
wideBtn.click()
Expand Down Expand Up @@ -280,11 +280,8 @@ if (isPageVideo() || isPagePlaylist()) {

// 监听网页全屏按钮出现
const listener = () => {
waitForEle(document, '.bpx-player-ctrl-web', (node: Node): boolean => {
return (
node instanceof HTMLElement &&
(node as HTMLElement).className.includes('bpx-player-ctrl-web')
)
waitForEle(document, '.bpx-player-ctrl-web', (node: HTMLElement): boolean => {
return node.className?.includes('bpx-player-ctrl-web')
}).then((webBtn) => {
if (webBtn) {
webBtn.addEventListener('click', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const isEleHide = (ele: HTMLElement) => {
export const waitForEle = async (
watchEle: HTMLElement | Document,
selector: string,
isTargetNode: (node: Node) => boolean,
isTargetNode: (node: HTMLElement) => boolean,
): Promise<HTMLElement | null> => {
let ele = watchEle.querySelector(selector) as HTMLElement | null
if (ele) {
Expand All @@ -54,7 +54,7 @@ export const waitForEle = async (
mutationList.forEach((mutation) => {
if (mutation.addedNodes) {
mutation.addedNodes.forEach((node) => {
if (isTargetNode(node)) {
if (node instanceof HTMLElement && isTargetNode(node)) {
obverser.disconnect()
ele = watchEle.querySelector(selector) as HTMLElement | null
resolve(ele)
Expand Down

0 comments on commit 2e82c96

Please sign in to comment.