Explorar o código

feat: 为Frame元素添加curl属性并注入base标签

在Frame元素的类型定义中新增curl属性,用于存储原始页面URL。在处理iframe的srcdoc时,根据curl注入<base href>标签,确保相对路径资源能正确解析。
lsc hai 1 semana
pai
achega
4fcb0c2872

+ 1 - 0
src/types/slides.ts

@@ -649,6 +649,7 @@ export interface PPTFrameElement extends PPTBaseElement {
   type: 'frame'
   url: string
   isHTML?: boolean,
+  curl?: string,
   toolType?: number,
   isDone?: boolean,
   webId?: string,

+ 2 - 1
src/views/Student/index.vue

@@ -1910,7 +1910,8 @@ const elementDone = async (element: any, slideIndex: number) => {
       _element = {
         ...element,
         isHTML: true,  
-        url: html
+        url: html,
+        curl: iframeSrc
       }
     }
   }

+ 15 - 0
src/views/components/element/FrameElement/BaseFrameElement.vue

@@ -240,6 +240,21 @@ const processedSrcDoc = computed(() => {
   for (const [pattern, replacement] of CDN_RULES) {
     html = html.replace(pattern, replacement)
   }
+  // 注入 <base href>:srcdoc 内相对路径资源需要基于原始页面 URL 解析
+  const baseHref = props.elementInfo.curl
+  if (baseHref && !/<base\s/i.test(html)) {
+    const safeHref = baseHref.replace(/"/g, '&quot;')
+    const baseTag = `<base href="${safeHref}">`
+    if (/<head[^>]*>/i.test(html)) {
+      html = html.replace(/<head[^>]*>/i, match => `${match}${baseTag}`)
+    }
+    else if (/<html[^>]*>/i.test(html)) {
+      html = html.replace(/<html[^>]*>/i, match => `${match}<head>${baseTag}</head>`)
+    }
+    else {
+      html = `<head>${baseTag}</head>${html}`
+    }
+  }
   return html
 })