Pass modifier keys to click event

This commit is contained in:
the1812 2021-03-02 17:58:07 +08:00
parent 64ea85a1c5
commit 9fe728043a
3 changed files with 14 additions and 10 deletions

2
min/keymap.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,9 +1,10 @@
export type KeyBindingAction = (context: { export interface KeyBindingActionContext {
binding: KeyBinding binding: KeyBinding
event: KeyboardEvent event: KeyboardEvent
isWatchlater: boolean isWatchlater: boolean
isMediaList: boolean isMediaList: boolean
}) => void }
export type KeyBindingAction = (context: KeyBindingActionContext) => void
export interface KeyBinding { export interface KeyBinding {
keys: string[] keys: string[]
action: KeyBindingAction action: KeyBindingAction

View File

@ -1,5 +1,5 @@
import { VideoSpeedController } from '../video-speed/video-speed-controller' import { VideoSpeedController } from '../video-speed/video-speed-controller'
import { KeyBinding, KeyBindingAction } from './key-bindings' import { KeyBinding, KeyBindingAction, KeyBindingActionContext } from './key-bindings'
const supportedUrls = [ const supportedUrls = [
'https://www.bilibili.com/bangumi/', 'https://www.bilibili.com/bangumi/',
@ -13,11 +13,14 @@ const supportedUrls = [
let config: { enable: boolean, bindings: KeyBinding[] } | undefined = undefined let config: { enable: boolean, bindings: KeyBinding[] } | undefined = undefined
if (supportedUrls.some(url => document.URL.startsWith(url))) { if (supportedUrls.some(url => document.URL.startsWith(url))) {
const clickElement = (target: string | HTMLElement) => { const clickElement = (target: string | HTMLElement) => {
return () => { return ({ event }: KeyBindingActionContext) => {
const mouseEvent = new MouseEvent('click', {
..._.pick(event, 'ctrlKey', 'shiftKey', 'altKey', 'metaKey')
})
if (typeof target === 'string') { if (typeof target === 'string') {
(dq(target) as HTMLElement)?.click() (dq(target) as HTMLElement)?.dispatchEvent(mouseEvent)
} else { } else {
target.click() target.dispatchEvent(mouseEvent)
} }
} }
} }
@ -92,8 +95,8 @@ if (supportedUrls.some(url => document.URL.startsWith(url))) {
showTip(`${after}%`, 'mdi-volume-high') showTip(`${after}%`, 'mdi-volume-high')
} }
}, },
mute: () => { mute: (context: KeyBindingActionContext) => {
clickElement('.bilibili-player-video-btn-volume .bilibili-player-iconfont-volume')() clickElement('.bilibili-player-video-btn-volume .bilibili-player-iconfont-volume')(context)
const isMute = unsafeWindow.player.isMute() const isMute = unsafeWindow.player.isMute()
if (isMute) { if (isMute) {
showTip('已静音', 'mdi-volume-off') showTip('已静音', 'mdi-volume-off')
@ -219,7 +222,7 @@ if (supportedUrls.some(url => document.URL.startsWith(url))) {
videoSpeedDecrease: 'shift < 《 arrowDown', videoSpeedDecrease: 'shift < 《 arrowDown',
videoSpeedReset: 'shift ? ', videoSpeedReset: 'shift ? ',
videoSpeedForget: 'shift : ', videoSpeedForget: 'shift : ',
takeScreenshot: 'ctrl alt c', takeScreenshot: 'ctrl [shift] alt c',
previousFrame: 'shift arrowLeft', previousFrame: 'shift arrowLeft',
nextFrame: 'shift arrowRight', nextFrame: 'shift arrowRight',
seekBegin: '0', seekBegin: '0',