[skip ci] web: VisibilityChange兼容Safari 14 14.5

This commit is contained in:
Xwite 2023-05-12 21:51:54 +08:00
parent 114b70c317
commit de516f8983
2 changed files with 18 additions and 10 deletions

View File

@ -58,7 +58,6 @@ const titleRef = ref();
const paragraphRef = ref();
const scrollToReadedLength = (length) => {
if (length === 0) return;
console.log("已读长度", length);
let paragraphIndex = chapterPos.value.findIndex(
(wordCount) => wordCount >= length
);

View File

@ -324,15 +324,18 @@ const saveReadingBookProgressToBrowser = (index, pos) => {
//
//
//
const syncBookProgress = () => {
console.log("page hide");
// tab https://developer.mozilla.org/zh-CN/docs/Web/API/Document/visibilitychange_event
const onVisibilityChange = () => {
if (!bookProgress.value) return;
// 使Fetch keep-alive navigator.sendBeacon
navigator.sendBeacon(
`${import.meta.env.VITE_API || location.origin}/saveBookProgress`,
JSON.stringify(bookProgress.value)
if (document.visibilityState == 'hidden') {
// Safari > 14 Safaripagehide event
document.removeEventListener("pagehide", onVisibilityChange);
// 使Fetch keep-alive navigator.sendBeacon
navigator.sendBeacon(
`${import.meta.env.VITE_API || location.origin}/saveBookProgress`,
JSON.stringify(bookProgress.value)
);
}
};
//
@ -478,7 +481,10 @@ onMounted(() => {
getContent(chapterIndex, true, chapterPos);
window.addEventListener("keyup", handleKeyPress);
window.addEventListener("visibilitychange", syncBookProgress);
// Safari < 14
document.addEventListener("visibilitychange", onVisibilityChange);
// Safari < 14.5
document.addEventListener("pagehide", onVisibilityChange);
//
scrollObserve.value = new IntersectionObserver(handleIScrollObserve, {
rootMargin: "-100% 0% 20% 0%",
@ -498,7 +504,10 @@ onMounted(() => {
onUnmounted(() => {
window.removeEventListener("keyup", handleKeyPress);
window.removeEventListener("visibilitychange", syncBookProgress);
// Safari < 14
document.removeEventListener("visibilitychange", onVisibilityChange);
// Safari < 14.5
document.removeEventListener("pagehide", onVisibilityChange);
readSettingsVisible.value = false;
popCataVisible.value = false;
scrollObserve.value?.disconnect();