Explorar el Código

某些情况下,window.cache 未定义。通过下载比例来判断。

Michael Wang hace 5 años
padre
commit
f2fa2ac07a
Se han modificado 1 ficheros con 22 adiciones y 3 borrados
  1. 22 3
      src/features/OnlineExam/Examing/QuestionAudio.vue

+ 22 - 3
src/features/OnlineExam/Examing/QuestionAudio.vue

@@ -49,14 +49,17 @@ export default {
         chromeVersion < 76 ||
         this.done ||
         !navigator.serviceWorker ||
-        !navigator.serviceWorker.controller
+        !navigator.serviceWorker.controller ||
+        // 在某些情况下,caches 没有定义,这时候不做cache. 暂时注释
+        // !window.caches ||
+        this.downloadPercent === 100
       );
     },
   },
   async created() {
     this.cacheInterval = setInterval(() => {
-      if (!this.done) {
-        caches
+      if (!this.done && window.caches) {
+        window.caches
           .open("audios")
           .then(caches => caches.match(this.src))
           .then(e => e && (this.done = true));
@@ -71,6 +74,22 @@ export default {
         );
     };
     navigator.serviceWorker.addEventListener("message", this.handleMSG);
+
+    let chromeVersion = window.navigator.userAgent
+      .split(" ")
+      .find(v => v.startsWith("Chrome/"));
+    if (chromeVersion) {
+      chromeVersion = +chromeVersion.replace("Chrome/", "").split(".")[0];
+    }
+    if (chromeVersion >= 76) {
+      if (!window.caches) {
+        window._hmt.push([
+          "_trackEvent",
+          "音频播放",
+          "chromeVersion >= 76 && window.caches 为空",
+        ]);
+      }
+    }
   },
   beforeDestroy() {
     clearInterval(this.cacheInterval);