@ -2,9 +2,11 @@
< div class = "project-page" >
< div class = "project-page" >
< iframe
< iframe
v - if = "previewUrl"
v - if = "previewUrl"
ref = "previewIframe"
: src = "previewUrl"
: src = "previewUrl"
class = "preview-iframe"
class = "preview-iframe"
sandbox = "allow-scripts allow-same-origin allow-forms"
sandbox = "allow-scripts allow-same-origin allow-forms"
@ load = "handleIframeLoad"
> < / iframe >
> < / iframe >
< div v-if ="!isInIframe" class="control-buttons" >
< div v-if ="!isInIframe" class="control-buttons" >
< button @click ="goHome" class = "control-btn back-btn" title = "BACK HOME" >
< button @click ="goHome" class = "control-btn back-btn" title = "BACK HOME" >
@ -110,6 +112,7 @@ const drawerVisible = ref(false);
const documentVisible = ref ( false ) ;
const documentVisible = ref ( false ) ;
const currentDocument = ref < Document | null > ( null ) ;
const currentDocument = ref < Document | null > ( null ) ;
const previewUrl = ref ( '' ) ;
const previewUrl = ref ( '' ) ;
const previewIframe = ref < HTMLIFrameElement | null > ( null ) ;
const isInIframe = computed ( ( ) => {
const isInIframe = computed ( ( ) => {
return window . self !== window . top ;
return window . self !== window . top ;
@ -175,6 +178,48 @@ const formatDate = (dateString: string) => {
return date . toLocaleString ( 'zh-CN' ) ;
return date . toLocaleString ( 'zh-CN' ) ;
} ;
} ;
const handleIframeLoad = ( ) => {
if ( ! previewIframe . value ) return ;
try {
const iframeDoc = previewIframe . value . contentDocument || previewIframe . value . contentWindow ? . document ;
if ( ! iframeDoc ) return ;
/ / 更 新 t i t l e
const iframeTitle = iframeDoc . title ;
if ( iframeTitle ) {
document . title = iframeTitle ;
}
/ / 更 新 f a v i c o n
const faviconLink = iframeDoc . querySelector ( 'link[rel="icon"], link[rel="shortcut icon"]' ) as HTMLLinkElement ;
if ( faviconLink ) {
const faviconHref = faviconLink . href ;
/ / 如 果 是 相 对 路 径 , 需 要 转 换 为 绝 对 路 径
let absoluteFaviconUrl = faviconHref ;
if ( faviconHref . startsWith ( '/' ) || ! faviconHref . startsWith ( 'http' ) ) {
const iframeBaseUrl = new URL ( previewUrl . value , window . location . origin ) ;
absoluteFaviconUrl = new URL ( faviconHref , iframeBaseUrl ) . href ;
}
/ / 移 除 旧 的 f a v i c o n
const existingFavicon = document . querySelector ( 'link[rel="icon"], link[rel="shortcut icon"]' ) ;
if ( existingFavicon ) {
existingFavicon . remove ( ) ;
}
/ / 添 加 新 的 f a v i c o n
const link = document . createElement ( 'link' ) ;
link . rel = 'icon' ;
link . href = absoluteFaviconUrl ;
document . head . appendChild ( link ) ;
}
} catch ( error ) {
/ / 如 果 由 于 跨 域 问 题 无 法 访 问 i f r a m e 内 容 , 静 默 失 败
console . warn ( '无法访问 iframe 内容:' , error ) ;
}
} ;
onMounted ( ( ) => {
onMounted ( ( ) => {
loadProject ( ) ;
loadProject ( ) ;
} ) ;
} ) ;