Browse Source

Add iframe load handling in Project.vue to update document title and favicon dynamically. This enhancement improves the user experience by ensuring the displayed content reflects the iframe's metadata.

deploy
dash 2 weeks ago
parent
commit
5ffab4ade2
  1. 45
      frontend/src/views/Project.vue

45
frontend/src/views/Project.vue

@ -2,9 +2,11 @@
<div class="project-page">
<iframe
v-if="previewUrl"
ref="previewIframe"
:src="previewUrl"
class="preview-iframe"
sandbox="allow-scripts allow-same-origin allow-forms"
@load="handleIframeLoad"
></iframe>
<div v-if="!isInIframe" class="control-buttons">
<button @click="goHome" class="control-btn back-btn" title="BACK HOME">
@ -110,6 +112,7 @@ const drawerVisible = ref(false);
const documentVisible = ref(false);
const currentDocument = ref<Document | null>(null);
const previewUrl = ref('');
const previewIframe = ref<HTMLIFrameElement | null>(null);
const isInIframe = computed(() => {
return window.self !== window.top;
@ -175,6 +178,48 @@ const formatDate = (dateString: string) => {
return date.toLocaleString('zh-CN');
};
const handleIframeLoad = () => {
if (!previewIframe.value) return;
try {
const iframeDoc = previewIframe.value.contentDocument || previewIframe.value.contentWindow?.document;
if (!iframeDoc) return;
// title
const iframeTitle = iframeDoc.title;
if (iframeTitle) {
document.title = iframeTitle;
}
// favicon
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;
}
// favicon
const existingFavicon = document.querySelector('link[rel="icon"], link[rel="shortcut icon"]');
if (existingFavicon) {
existingFavicon.remove();
}
// favicon
const link = document.createElement('link');
link.rel = 'icon';
link.href = absoluteFaviconUrl;
document.head.appendChild(link);
}
} catch (error) {
// 访 iframe
console.warn('无法访问 iframe 内容:', error);
}
};
onMounted(() => {
loadProject();
});

Loading…
Cancel
Save