Skip to content

Commit bd29ebf

Browse files
committed
feat: ✨ 进入阅读模式后将不再自动熄屏
1 parent a4eddd6 commit bd29ebf

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/components/Manga/hooks/useInit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ export const useInit = (props: MangaProps) => {
7070
state.prop.onExit = (isEnd?: boolean | Event) => {
7171
playAnimation(refs.exit);
7272
props.onExit?.(Boolean(isEnd));
73-
document.exitFullscreen();
7473
setState((draftState) => {
7574
if (isEnd) draftState.activePageIndex = 0;
7675
draftState.show.endPage = undefined;
7776
});
77+
if (document.fullscreenElement) document.exitFullscreen();
7878
};
7979
},
8080
onPrev(state) {

src/helper/other.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,3 +615,31 @@ export abstract class AnimationFrame {
615615
this.animationId = 0;
616616
};
617617
}
618+
619+
/** 锁定屏幕禁止自动熄屏 */
620+
export class WakeLock {
621+
isSupported = false;
622+
623+
lock: WakeLockSentinel | null = null;
624+
625+
constructor() {
626+
if (!('wakeLock' in navigator)) return;
627+
this.isSupported = true;
628+
}
629+
630+
on = async () => {
631+
if (!this.isSupported) return null;
632+
try {
633+
this.lock = await navigator.wakeLock.request('screen');
634+
return this.lock.released;
635+
} catch {
636+
return false;
637+
}
638+
};
639+
640+
off = async () => {
641+
if (!this.lock) return;
642+
await this.lock.release();
643+
this.lock = null;
644+
};
645+
}

src/userscript/useComponents/Manga.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
createRootMemo,
99
mountComponents,
1010
querySelector,
11+
WakeLock,
1112
} from 'helper';
1213

1314
import { DownloadButton } from '../../components/DownloadButton';
@@ -68,6 +69,8 @@ export const useManga = async (initProps?: Partial<MangaProps>) => {
6869
const htmlStyle = document.documentElement.style;
6970
let lastOverflow = htmlStyle.overflow;
7071

72+
const wakeLock = new WakeLock();
73+
7174
createEffectOn(
7275
createRootMemo(() => props.show && props.imgList.length > 0),
7376
(show) => {
@@ -77,10 +80,12 @@ export const useManga = async (initProps?: Partial<MangaProps>) => {
7780
htmlStyle.setProperty('overflow', 'hidden', 'important');
7881
htmlStyle.setProperty('scrollbar-width', 'none', 'important');
7982
if (store.option.autoFullscreen) refs.root.requestFullscreen();
83+
wakeLock.on();
8084
} else {
8185
dom.removeAttribute('show');
8286
htmlStyle.overflow = lastOverflow;
8387
htmlStyle.removeProperty('scrollbar-width');
88+
wakeLock.off();
8489
}
8590
},
8691
{ defer: true },

0 commit comments

Comments
 (0)