|
1 | 1 | import { bv2av } from '@mgdn/bvid' |
| 2 | +import { matchesKeyboardEvent } from '@tanstack/react-hotkeys' |
2 | 3 | import { delay } from 'es-toolkit' |
3 | 4 | import ms from 'ms' |
4 | 5 | import { baseDebug } from '$common' |
@@ -129,35 +130,44 @@ function registerAddToFavCommand() { |
129 | 130 | GM.registerMenuCommand?.('⭐️ 加入收藏', () => addToFav()) |
130 | 131 | } |
131 | 132 | async function setupCustomFavPicker() { |
132 | | - if (!settings.fav.useCustomFavPicker.onPlayPage) return |
133 | 133 | if (!getCurrentPageBvid()) return |
| 134 | + const willUseCustomFavPicker = () => settings.fav.useCustomFavPicker.onPlayPage |
| 135 | + |
| 136 | + // setup keyboard shortcut |
| 137 | + // Shift+E: always on |
| 138 | + // E: only when willUseCustomFavPicker |
134 | 139 | document.addEventListener( |
135 | 140 | 'keydown', |
136 | 141 | (e) => { |
137 | | - if (e.key !== 'e') return |
138 | | - if (shouldDisableShortcut()) return |
139 | | - const target = e.target as HTMLElement |
140 | | - if (target.closest('bili-comments')) return // emit from a <bili-comments> element |
141 | | - e.stopImmediatePropagation() |
142 | | - e.preventDefault() |
143 | | - addToFav() |
| 142 | + if (matchesKeyboardEvent(e, 'Shift+E') || (willUseCustomFavPicker() && matchesKeyboardEvent(e, 'E'))) { |
| 143 | + if (!getCurrentPageBvid()) return |
| 144 | + if (shouldDisableShortcut()) return |
| 145 | + const target = e.target as HTMLElement |
| 146 | + if (target.closest('bili-comments')) return // emit from a <bili-comments> element |
| 147 | + e.stopImmediatePropagation() |
| 148 | + e.preventDefault() |
| 149 | + addToFav() |
| 150 | + } |
144 | 151 | }, |
145 | 152 | { capture: true }, |
146 | 153 | ) |
147 | 154 |
|
148 | | - const el = await poll(() => document.querySelector<HTMLDivElement>('.video-fav.video-toolbar-left-item'), { |
149 | | - interval: 100, |
150 | | - timeout: 5_000, |
151 | | - }) |
152 | | - el?.addEventListener( |
153 | | - 'click', |
154 | | - (e) => { |
155 | | - e.stopImmediatePropagation() |
156 | | - e.preventDefault() |
157 | | - addToFav() |
158 | | - }, |
159 | | - { capture: true }, |
160 | | - ) |
| 155 | + if (willUseCustomFavPicker()) { |
| 156 | + const el = await poll(() => document.querySelector<HTMLDivElement>('.video-fav.video-toolbar-left-item'), { |
| 157 | + interval: 100, |
| 158 | + timeout: 5_000, |
| 159 | + }) |
| 160 | + el?.addEventListener( |
| 161 | + 'click', |
| 162 | + (e) => { |
| 163 | + if (!willUseCustomFavPicker() || !getCurrentPageBvid()) return |
| 164 | + e.stopImmediatePropagation() |
| 165 | + e.preventDefault() |
| 166 | + addToFav() |
| 167 | + }, |
| 168 | + { capture: true }, |
| 169 | + ) |
| 170 | + } |
161 | 171 | } |
162 | 172 |
|
163 | 173 | async function addToFav(sourceFavFolderIds?: number[] | undefined) { |
|
0 commit comments