Skip to content

Commit

Permalink
fix: 🐛 修复 eh 画廊内翻页快捷键失效的 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hymbz committed Jul 15, 2024
1 parent 1ae2322 commit dbcdb53
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ Cotrans 也有自己的油猴脚本 —— 「[Cotrans 漫画/图片翻译器](h

### 快捷键翻页

在列表页和详情页使用左右方向键翻页。
在列表页和详情页使用左右方向键翻页。以及使用上下方向键进行标签投票。

## nhentai

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ Cotrans 也有自己的油猴脚本 —— 「[Cotrans 漫画/图片翻译器](h

### 快捷键翻页

在列表页和详情页使用左右方向键翻页。
在列表页和详情页使用左右方向键翻页。以及使用上下方向键进行标签投票。

## nhentai

Expand Down
13 changes: 0 additions & 13 deletions src/helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,6 @@ export const saveAs = (blob: Blob, name = 'download') => {
setTimeout(() => a.dispatchEvent(new MouseEvent('click')));
};

/** 监听键盘事件 */
export const linstenKeyup = (handler: (e: KeyboardEvent) => unknown) =>
window.addEventListener('keyup', (e) => {
// 跳过输入框的键盘事件
switch ((e.target as HTMLElement).tagName) {
case 'INPUT':
case 'TEXTAREA':
return;
}

handler(e);
});

/** 滚动页面到指定元素的所在位置 */
export const scrollIntoView = (
selector: string,
Expand Down
45 changes: 34 additions & 11 deletions src/site/ehentai/hotkeys.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,58 @@
import { linstenKeyup, querySelector } from 'main';
import { querySelector } from 'main';

import { type PageType } from '.';

/** 监听键盘事件 */
export const linstenKeydown = (handler: (e: KeyboardEvent) => unknown) =>
window.addEventListener('keydown', (e) => {
// 跳过输入框的键盘事件
switch ((e.target as HTMLElement).tagName) {
case 'INPUT':
case 'TEXTAREA':
return;
}
return handler(e);
});

/** 快捷键翻页 */
export const hotkeysPageTurn = (pageType: PageType) => {
if (pageType === 'gallery') {
linstenKeyup((e) => {
linstenKeydown((e) => {
switch (e.key) {
case 'ArrowRight':
case 'd':
querySelector('#dnext')?.click();
break;
e.preventDefault();
return querySelector('.ptt td:last-child:not(.ptdd)')?.click();

case 'ArrowLeft':
case 'a':
querySelector('#dprev')?.click();
break;
e.preventDefault();
return querySelector('.ptt td:first-child:not(.ptdd)')?.click();
}

// 使用上下方向键进行投票
if (!unsafeWindow.selected_tagid) return;
switch (e.key) {
case 'ArrowUp':
e.preventDefault();
return unsafeWindow?.tag_vote_up();
case 'ArrowDown':
e.preventDefault();
return unsafeWindow?.tag_vote_down();
}
});
} else {
linstenKeyup((e) => {
linstenKeydown((e) => {
switch (e.key) {
case 'ArrowRight':
case 'd':
querySelector('#unext')?.click();
break;
e.preventDefault();
return querySelector('#unext')?.click();

case 'ArrowLeft':
case 'a':
querySelector('#uprev')?.click();
break;
e.preventDefault();
return querySelector('#uprev')?.click();
}
});
}
Expand Down

0 comments on commit dbcdb53

Please sign in to comment.