Эх сурвалжийг харах

web: 移除Safari<14.5兼容处理

Xwite 3 жил өмнө
parent
commit
4529f23f78

+ 12 - 2
modules/web/src/api/index.js

@@ -22,9 +22,18 @@ ajax.interceptors.response.use((response) => response, APIExceptionHandler);
 const getReadConfig = () => ajax.get("/getReadConfig");
 const saveReadConfig = (config) => ajax.post("/saveReadConfig", config);
 
-const saveBookProcess = (bookProgress) =>
+const saveBookProgress = (bookProgress) =>
   ajax.post("/saveBookProgress", bookProgress);
 
+const saveBookProgressWithBeacon = (bookProgress) => {
+  if (!bookProgress) return;
+  // 常规请求可能会被取消 使用Fetch keep-alive 或者 navigator.sendBeacon
+  navigator.sendBeacon(
+    `${import.meta.env.VITE_API || location.origin}/saveBookProgress`,
+    JSON.stringify(bookProgress)
+  );
+};
+
 const getBookShelf = () => ajax.get("/getBookshelf");
 
 const getChapterList = (/** @type {string} */ bookUrl) =>
@@ -114,7 +123,8 @@ const debug = (
 export default {
   getReadConfig,
   saveReadConfig,
-  saveBookProcess,
+  saveBookProgress,
+  saveBookProgressWithBeacon,
   getBookShelf,
   getChapterList,
   getBookContent,

+ 0 - 1
modules/web/src/plugins/utils.js

@@ -22,7 +22,6 @@ export function getImageFromLegado(src) {
   );
 }
 // @ts-ignore
-
 export const dateFormat = (/** @type {number} */ t) => {
   let time = new Date().getTime();
   let offset = Math.floor((time - t) / 1000);

+ 1 - 1
modules/web/src/store/bookStore.js

@@ -100,7 +100,7 @@ export const useBookStore = defineStore("book", {
     //保存进度到app
     async saveBookProgress() {
       if (!this.bookProgress) return Promise.resolve();
-      return API.saveBookProcess(this.bookProgress);
+      return API.saveBookProgress(this.bookProgress);
     },
   },
 });

+ 8 - 15
modules/web/src/views/BookChapter.vue

@@ -312,21 +312,18 @@ const saveReadingBookProgressToBrowser = (index, pos) => {
 };
 
 // 进度同步
-// 返回同步 同步请求会在获取书架前完成
-// 关闭页面 切换tab 返回桌面 等操作 https://developer.mozilla.org/zh-CN/docs/Web/API/Document/visibilitychange_event
+// 返回导航变化 同步请求会在获取书架前完成
+
+/**
+ * VisibilityChange https://developer.mozilla.org/zh-CN/docs/Web/API/Document/visibilitychange_event
+ * 监听关闭页面 切换tab 返回桌面 等操作
+ * 注意不用监听点击链接导航变化 不对Safari<14.5兼容处理
+**/
 const onVisibilityChange = () => {
-  if (!bookProgress.value) return;
   if (document.visibilityState == "hidden") {
-    // Safari > 14 和 非Safari移除额外pagehide event
-    document.removeEventListener("pagehide", onVisibilityChange);
-    // 常规请求可能会被取消 使用Fetch keep-alive 或者 navigator.sendBeacon
-    navigator.sendBeacon(
-      `${import.meta.env.VITE_API || location.origin}/saveBookProgress`,
-      JSON.stringify(bookProgress.value)
-    );
+    API.saveBookProgressWithBeacon(bookProgress.value);
   }
 };
-
 // 定时同步
 
 // 章节切换
@@ -470,8 +467,6 @@ onMounted(() => {
         window.addEventListener("keyup", handleKeyPress);
         // 兼容Safari < 14
         document.addEventListener("visibilitychange", onVisibilityChange);
-        // 兼容Safari < 14.5
-        document.addEventListener("pagehide", onVisibilityChange);
         //监听底部加载
         scrollObserve.value = new IntersectionObserver(handleIScrollObserve, {
           rootMargin: "-100% 0% 20% 0%",
@@ -493,8 +488,6 @@ onUnmounted(() => {
   window.removeEventListener("keyup", handleKeyPress);
   // 兼容Safari < 14
   document.removeEventListener("visibilitychange", onVisibilityChange);
-  // 兼容Safari < 14.5
-  document.removeEventListener("pagehide", onVisibilityChange);
   readSettingsVisible.value = false;
   popCataVisible.value = false;
   scrollObserve.value?.disconnect();