Browse Source

refactor(student): 注释掉iframe内容获取的冗余逻辑

简化了处理iframe内容的代码,将整个try-catch块注释掉,保留了核心的回退逻辑,减少了不必要的代码执行和日志输出
lsc 1 week ago
parent
commit
8afa7ace50
1 changed files with 91 additions and 91 deletions
  1. 91 91
      src/views/Student/index.vue

+ 91 - 91
src/views/Student/index.vue

@@ -1850,101 +1850,101 @@ const elementDone = async (element: any, slideIndex: number) => {
     hasIframe = true
                   
     // 先尝试获取iframe的contentWindow,如果获取不到再使用HTML方式
-    try {
-      // 创建一个临时的iframe来测试是否能获取contentWindow
-      const tempIframe = document.createElement('iframe')
-      // 不用 display:none:iPad Safari/WKWebView 会对隐藏 iframe 资源节流,
-      // onload 迟迟不触发导致超时误判为"禁止嵌入",误入抓 HTML 路径(跨站 Cookie 被 ITP 拦截必然失败)
-      tempIframe.style.position = 'absolute'
-      tempIframe.style.left = '-9999px'
-      tempIframe.style.top = '-9999px'
-      tempIframe.style.width = '1px'
-      tempIframe.style.height = '1px'
-      tempIframe.src = iframeSrc
+    // try {
+    //   // 创建一个临时的iframe来测试是否能获取contentWindow
+    //   const tempIframe = document.createElement('iframe')
+    //   // 不用 display:none:iPad Safari/WKWebView 会对隐藏 iframe 资源节流,
+    //   // onload 迟迟不触发导致超时误判为"禁止嵌入",误入抓 HTML 路径(跨站 Cookie 被 ITP 拦截必然失败)
+    //   tempIframe.style.position = 'absolute'
+    //   tempIframe.style.left = '-9999px'
+    //   tempIframe.style.top = '-9999px'
+    //   tempIframe.style.width = '1px'
+    //   tempIframe.style.height = '1px'
+    //   tempIframe.src = iframeSrc
                     
-      // 先将临时iframe添加到body,否则onload事件不会触发
-      document.body.appendChild(tempIframe)
-      // 等待iframe加载完成
-      await new Promise((resolve, reject) => {
-        tempIframe.onload = resolve
-        tempIframe.onerror = reject
-        // iPad 网络/渲染较慢,超时放宽到 12 秒
-        setTimeout(() => reject(new Error('Timeout')), 12000)
-      })
+    //   // 先将临时iframe添加到body,否则onload事件不会触发
+    //   document.body.appendChild(tempIframe)
+    //   // 等待iframe加载完成
+    //   await new Promise((resolve, reject) => {
+    //     tempIframe.onload = resolve
+    //     tempIframe.onerror = reject
+    //     // iPad 网络/渲染较慢,超时放宽到 12 秒
+    //     setTimeout(() => reject(new Error('Timeout')), 12000)
+    //   })
                     
-      // 尝试获取contentDocument(标准属性:跨域返回 null 不抛异常;
-      // 不能用 contentWindow.document,iPad Safari 跨域访问会直接抛 SecurityError,又会误入抓 HTML 路径)
-      if (tempIframe.contentDocument) {
-        console.log(`iframe ${iframeSrc} 可以获取contentWindow,使用直接加载方式`)
-        // 移除临时iframe
-        document.body.removeChild(tempIframe)
+    //   // 尝试获取contentDocument(标准属性:跨域返回 null 不抛异常;
+    //   // 不能用 contentWindow.document,iPad Safari 跨域访问会直接抛 SecurityError,又会误入抓 HTML 路径)
+    //   if (tempIframe.contentDocument) {
+    //     console.log(`iframe ${iframeSrc} 可以获取contentWindow,使用直接加载方式`)
+    //     // 移除临时iframe
+    //     document.body.removeChild(tempIframe)
                       
-        _element = {
-          ...element,
-          isHTML: false,
-          url: iframeSrc
-        }
-      }
-      else {
-      // 加载完成但无法获取contentWindow,也要移除iframe
-        document.body.removeChild(tempIframe)
-      }
-    }
-    catch (error) {
-      console.log(`iframe ${iframeSrc} 无法获取contentWindow,使用HTML方式:`, error)
+    //     _element = {
+    //       ...element,
+    //       isHTML: false,
+    //       url: iframeSrc
+    //     }
+    //   }
+    //   else {
+    //   // 加载完成但无法获取contentWindow,也要移除iframe
+    //     document.body.removeChild(tempIframe)
+    //   }
+    // }
+    // catch (error) {
+    //   console.log(`iframe ${iframeSrc} 无法获取contentWindow,使用HTML方式:`, error)
                   
-      // 如果无法获取contentWindow,使用HTML方式
-      let html = null
-      try {
-        html = await api.getHTML(iframeSrc)
-        console.log('getHTML 成功获取内容:', html)
-      }
-      catch (error) {
-        console.log(`getHTML 失败,尝试使用 getFile:`, error)
-        try {
-          const fileData = await getFile(iframeSrc)
-          if (fileData && fileData.data) {
-            const uint8Array = new Uint8Array(fileData.data)
-            html = new TextDecoder('utf-8').decode(uint8Array)
-            console.log('getFile 成功获取内容:', html)
-          }
-        }
-        catch (error2) {
-          console.log(`getFile 失败,尝试使用 getHTML:`, error2)
-          try {
-            const fileData2 = await getFile2(iframeSrc)
-            if (fileData2 && fileData2.data) {
-              const uint8Array = new Uint8Array(fileData2.data)
-              html = new TextDecoder('utf-8').decode(uint8Array)
-              console.log('getFile2 成功获取内容:', html)
-            }
-          }
-          catch (error3) {
-            console.error('getFile2 也失败:', error3)
-            console.error('无法获取内容: getFile、getFile2 和 getHTML 都失败了')
-          }
-        }
-      }
-      console.log(`处理幻灯片 ${slideIndex + 1} 中的iframe链接:`, iframeSrc)
-      if (html) {
-        _element = {
-          ...element,
-          isHTML: true,
-          url: html,
-          curl: iframeSrc
-        }
-      }
-      else {
-        // getHTML/getFile/getFile2 全失败:不能置空 url 硬切 srcdoc(会白屏),
-        // 回退为直接加载原始链接,至少保持与进页面时一致的显示效果
-        console.warn(`幻灯片 ${slideIndex + 1} 未获取到HTML内容,回退为直接加载:`, iframeSrc)
-        _element = {
-          ...element,
-          isHTML: false,
-          url: iframeSrc
-        }
-      }
-    }
+    //   // 如果无法获取contentWindow,使用HTML方式
+    //   let html = null
+    //   try {
+    //     html = await api.getHTML(iframeSrc)
+    //     console.log('getHTML 成功获取内容:', html)
+    //   }
+    //   catch (error) {
+    //     console.log(`getHTML 失败,尝试使用 getFile:`, error)
+    //     try {
+    //       const fileData = await getFile(iframeSrc)
+    //       if (fileData && fileData.data) {
+    //         const uint8Array = new Uint8Array(fileData.data)
+    //         html = new TextDecoder('utf-8').decode(uint8Array)
+    //         console.log('getFile 成功获取内容:', html)
+    //       }
+    //     }
+    //     catch (error2) {
+    //       console.log(`getFile 失败,尝试使用 getHTML:`, error2)
+    //       try {
+    //         const fileData2 = await getFile2(iframeSrc)
+    //         if (fileData2 && fileData2.data) {
+    //           const uint8Array = new Uint8Array(fileData2.data)
+    //           html = new TextDecoder('utf-8').decode(uint8Array)
+    //           console.log('getFile2 成功获取内容:', html)
+    //         }
+    //       }
+    //       catch (error3) {
+    //         console.error('getFile2 也失败:', error3)
+    //         console.error('无法获取内容: getFile、getFile2 和 getHTML 都失败了')
+    //       }
+    //     }
+    //   }
+    //   console.log(`处理幻灯片 ${slideIndex + 1} 中的iframe链接:`, iframeSrc)
+    //   if (html) {
+    //     _element = {
+    //       ...element,
+    //       isHTML: true,
+    //       url: html,
+    //       curl: iframeSrc
+    //     }
+    //   }
+    //   else {
+    //     // getHTML/getFile/getFile2 全失败:不能置空 url 硬切 srcdoc(会白屏),
+    //     // 回退为直接加载原始链接,至少保持与进页面时一致的显示效果
+    //     console.warn(`幻灯片 ${slideIndex + 1} 未获取到HTML内容,回退为直接加载:`, iframeSrc)
+    //     _element = {
+    //       ...element,
+    //       isHTML: false,
+    //       url: iframeSrc
+    //     }
+    //   }
+    // }
   }
 
   return {