Skip to content

Commit

Permalink
feat: rule items hotkey
Browse files Browse the repository at this point in the history
  • Loading branch information
festoney8 committed Oct 20, 2024
1 parent 2855014 commit b3d60fc
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { waitForHead } from '../utils/init'
import { log } from '../utils/logger'
import { loadFilters } from './filters'
import { loadRules, loadStyles } from './rules'
import { loadRules, loadRulesHotKey, loadStyles } from './rules'

export const loadModules = () => {
waitForHead().then(() => {
Expand All @@ -10,6 +10,7 @@ export const loadModules = () => {
})

loadRules()
loadRulesHotKey()
log('loadRules done')

loadFilters()
Expand Down
4 changes: 2 additions & 2 deletions src/modules/rules/bangumi/groups/playerLayout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { unsafeWindow } from '$'
import { Item } from '../../../../types/item'
import { waitForEle } from '../../../../utils/tool'
import { isFirefox, waitForEle } from '../../../../utils/tool'

/**
* Firefox DOMMouseScroll无法被stopImmediatePropagation
Expand Down Expand Up @@ -77,7 +77,7 @@ export const bangumiPlayerLayoutItems: Item[] = [
name: '全屏时 页面可滚动 (实验功能)',
description: ['播放器内滚轮调节音量失效', '点击全屏按钮生效,双击全屏无效', 'Firefox 不适用'],
enableFn: async () => {
if (!navigator.userAgent.toLocaleLowerCase().includes('chrome')) {
if (isFirefox()) {
return
}

Expand Down
4 changes: 0 additions & 4 deletions src/modules/rules/homepage/groups/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ export const homepageLayoutItems: Item[] = [
id: 'homepage-layout-5-column',
name: '5 列布局',
},
{
id: 'homepage-layout-6-column',
name: '6 列布局',
},
],
},
{
Expand Down
77 changes: 77 additions & 0 deletions src/modules/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { spaceGroups } from './space'
import { videoGroups } from './video'
import { watchlaterGroups } from './watchlater'

import { useMagicKeys } from '@vueuse/core'
import bangumiStyle from './bangumi/index.scss?inline'
import channelStyle from './channel/index.scss?inline'
import commentStyle from './comment/index.scss?inline'
Expand Down Expand Up @@ -337,3 +338,79 @@ const loadListItem = (item: IListItem) => {
document.documentElement.setAttribute(value, '')
}
}

/**
* 快捷键 Alt + B,快速禁用无函数作用的 rule item
*/
export const loadRulesHotKey = () => {
try {
const availableItemIds = new Set<string>()
for (const rule of rules) {
if (!rule.checkFn()) {
continue
}
for (const group of rule.groups) {
for (const item of group.items) {
switch (item.type) {
case 'switch':
if (!item.enableFn) {
availableItemIds.add(item.id)
}
break
case 'number':
case 'string':
availableItemIds.add(item.id)
break
case 'list':
item.options.forEach((v) => {
availableItemIds.add(v.id)
})
break
}
}
}
}

// 管理 html 节点的 attributes
let isOn = false
const disableSign = '_bili_cleaner_disable_'
const toggle = () => {
if (!isOn) {
const attrs: string[] = []
for (const attr of document.documentElement.attributes) {
if (availableItemIds.has(attr.name)) {
attrs.push(attr.name)
}
}
for (const attr of attrs) {
document.documentElement.removeAttribute(attr)
document.documentElement.setAttribute(disableSign + attr, '')
}
} else {
const attrs: string[] = []
for (const attr of document.documentElement.attributes) {
if (attr.name.includes(disableSign)) {
attrs.push(attr.name)
}
}
for (const attr of attrs) {
document.documentElement.removeAttribute(attr)
document.documentElement.setAttribute(attr.replace(disableSign, ''), '')
}
}
isOn = !isOn
}

useMagicKeys({
passive: false,
onEventFired(e) {
if (e.type === 'keydown' && e.altKey && e.key.toLocaleLowerCase() === 'b') {
e.preventDefault()
toggle()
}
},
})
} catch (err) {
error(`loadRulesHotKey error`, err)
}
}
4 changes: 2 additions & 2 deletions src/modules/rules/video/groups/playerLayout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { unsafeWindow } from '$'
import { Item } from '../../../../types/item'
import { waitForEle } from '../../../../utils/tool'
import { isFirefox, waitForEle } from '../../../../utils/tool'
import { wideScreenManager } from '../../../../utils/widePlayer'

const disableAdjustVolume = () => {}
Expand Down Expand Up @@ -60,7 +60,7 @@ export const videoPlayerLayoutItems: Item[] = [
name: '全屏时 页面可滚动 (实验功能)',
description: ['播放器内滚轮调节音量失效', '点击全屏按钮时生效,双击全屏无效', 'Firefox 不适用'],
enableFn: async () => {
if (!navigator.userAgent.toLocaleLowerCase().includes('chrome')) {
if (isFirefox()) {
return
}
// 禁用滚动调音量
Expand Down
8 changes: 8 additions & 0 deletions src/utils/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,11 @@ export const waitForEle = async (
export const orderedUniq = <T = unknown>(arr: T[]): T[] => {
return Array.from(new Set(arr))
}

/**
* 判断是否为 Firefox 浏览器
* @returns boolean
*/
export const isFirefox = (): boolean => {
return navigator.userAgent.toLocaleLowerCase().includes('firefox')
}

0 comments on commit b3d60fc

Please sign in to comment.