mirror of
https://github.com/hi2shark/nazhua.git
synced 2026-01-15 16:50:42 +08:00
🪄 尝试优化异步加载的配置执行,使其生效
This commit is contained in:
parent
640fd787a2
commit
ec3b5cb5ea
@ -54,6 +54,7 @@ const store = useStore();
|
|||||||
const footerSlogan = computed(() => decodeURIComponent(config.nazhua?.footerSlogan || ''));
|
const footerSlogan = computed(() => decodeURIComponent(config.nazhua?.footerSlogan || ''));
|
||||||
|
|
||||||
const dynamicContentRef = ref();
|
const dynamicContentRef = ref();
|
||||||
|
const executedScripts = ref(new Set()); // 记录已执行的脚本,避免重复执行
|
||||||
|
|
||||||
const dynamicContent = computed(() => {
|
const dynamicContent = computed(() => {
|
||||||
if (store.state.setting?.config?.custom_code) {
|
if (store.state.setting?.config?.custom_code) {
|
||||||
@ -69,24 +70,69 @@ const dynamicContent = computed(() => {
|
|||||||
const executeScripts = () => {
|
const executeScripts = () => {
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
if (!dynamicContentRef.value) return;
|
if (!dynamicContentRef.value) return;
|
||||||
|
|
||||||
const scripts = dynamicContentRef.value.querySelectorAll('script');
|
const scripts = dynamicContentRef.value.querySelectorAll('script');
|
||||||
|
|
||||||
scripts.forEach((script) => {
|
scripts.forEach((script) => {
|
||||||
const newScript = document.createElement('script');
|
try {
|
||||||
newScript.type = 'text/javascript';
|
// 生成脚本唯一标识,避免重复执行
|
||||||
if (script.src) {
|
const scriptIdentifier = script.src || script.textContent || '';
|
||||||
newScript.src = script.src; // 拷贝外部脚本的 src
|
if (!scriptIdentifier || executedScripts.value.has(scriptIdentifier)) {
|
||||||
} else {
|
return;
|
||||||
newScript.textContent = script.textContent; // 拷贝内联脚本
|
}
|
||||||
|
|
||||||
|
const newScript = document.createElement('script');
|
||||||
|
newScript.type = script.type || 'text/javascript';
|
||||||
|
|
||||||
|
// 复制所有相关属性
|
||||||
|
if (script.async !== undefined) newScript.async = script.async;
|
||||||
|
if (script.defer !== undefined) newScript.defer = script.defer;
|
||||||
|
if (script.crossOrigin) newScript.crossOrigin = script.crossOrigin;
|
||||||
|
if (script.integrity) newScript.integrity = script.integrity;
|
||||||
|
if (script.noModule !== undefined) newScript.noModule = script.noModule;
|
||||||
|
if (script.referrerPolicy) newScript.referrerPolicy = script.referrerPolicy;
|
||||||
|
|
||||||
|
if (script.src) {
|
||||||
|
// 外部脚本:监听加载完成事件
|
||||||
|
newScript.src = script.src;
|
||||||
|
newScript.onload = () => {
|
||||||
|
executedScripts.value.add(scriptIdentifier);
|
||||||
|
};
|
||||||
|
newScript.onerror = (error) => {
|
||||||
|
console.error('Failed to load external script:', script.src, error);
|
||||||
|
};
|
||||||
|
document.body.appendChild(newScript);
|
||||||
|
} else {
|
||||||
|
// 内联脚本:直接执行
|
||||||
|
newScript.textContent = script.textContent;
|
||||||
|
document.body.appendChild(newScript);
|
||||||
|
executedScripts.value.add(scriptIdentifier);
|
||||||
|
// 内联脚本执行后可以安全移除
|
||||||
|
document.body.removeChild(newScript);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error executing dynamic script:', error);
|
||||||
}
|
}
|
||||||
document.body.appendChild(newScript);
|
|
||||||
document.body.removeChild(newScript); // 可选:移除以保持整洁
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
watch(dynamicContent, () => {
|
// 清理已执行脚本的记录(当内容变化时)
|
||||||
if (dynamicContent.value) {
|
const cleanupScripts = () => {
|
||||||
executeScripts();
|
executedScripts.value.clear();
|
||||||
|
};
|
||||||
|
|
||||||
|
watch(dynamicContent, (newVal, oldVal) => {
|
||||||
|
// 内容变化时,清理旧的执行记录
|
||||||
|
if (newVal !== oldVal) {
|
||||||
|
cleanupScripts();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newVal) {
|
||||||
|
// 确保 DOM 已更新
|
||||||
|
nextTick(() => {
|
||||||
|
executeScripts();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user