mirror of
https://github.com/woodchen-ink/nezha-dash-v1.git
synced 2025-07-18 17:41:56 +08:00
commit
115fd59ec8
@ -21,11 +21,14 @@ export const InjectContext = (content: string) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const executeInlineScript = (scriptElement: HTMLScriptElement): void => {
|
const executeInlineScript = (scriptContent: string): Promise<void> => {
|
||||||
|
return new Promise((resolve) => {
|
||||||
const script = document.createElement("script")
|
const script = document.createElement("script")
|
||||||
script.textContent = scriptElement.textContent
|
script.textContent = scriptContent
|
||||||
script.setAttribute(INJECTION_MARK, "true") // 添加标识
|
script.setAttribute(INJECTION_MARK, "true") // 添加标识
|
||||||
document.body.appendChild(script)
|
document.body.appendChild(script)
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadStyle = (styleElement: HTMLStyleElement): Promise<void> => {
|
const loadStyle = (styleElement: HTMLStyleElement): Promise<void> => {
|
||||||
@ -56,8 +59,8 @@ export const InjectContext = (content: string) => {
|
|||||||
// 加载外部脚本
|
// 加载外部脚本
|
||||||
return loadExternalScript(scriptElement)
|
return loadExternalScript(scriptElement)
|
||||||
} else {
|
} else {
|
||||||
// 推迟执行内联脚本,后续手动执行
|
// 执行内联脚本
|
||||||
return Promise.resolve()
|
return executeInlineScript(scriptElement.textContent || "")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
STYLE: (element) => loadStyle(element as HTMLStyleElement),
|
STYLE: (element) => loadStyle(element as HTMLStyleElement),
|
||||||
@ -77,28 +80,20 @@ export const InjectContext = (content: string) => {
|
|||||||
// 开始注入前清理已有资源
|
// 开始注入前清理已有资源
|
||||||
cleanInjectedResources()
|
cleanInjectedResources()
|
||||||
|
|
||||||
const externalScriptQueue: Promise<void>[] = []
|
const executeSequentially = async () => {
|
||||||
|
for (const node of Array.from(tempDiv.childNodes)) {
|
||||||
Array.from(tempDiv.childNodes).forEach((node) => {
|
|
||||||
if (node.nodeType === Node.ELEMENT_NODE) {
|
if (node.nodeType === Node.ELEMENT_NODE) {
|
||||||
const element = node as HTMLElement
|
const element = node as HTMLElement
|
||||||
if (element.tagName === "SCRIPT" && !(element as HTMLScriptElement).src) {
|
|
||||||
// 直接执行内联脚本
|
|
||||||
executeInlineScript(element as HTMLScriptElement)
|
|
||||||
} else {
|
|
||||||
const handler = handlers[element.tagName] || handlers.DEFAULT
|
const handler = handlers[element.tagName] || handlers.DEFAULT
|
||||||
externalScriptQueue.push(handler(element))
|
await handler(element) // 按顺序等待当前脚本或资源完成处理
|
||||||
}
|
|
||||||
} else if (node.nodeType === Node.TEXT_NODE) {
|
} else if (node.nodeType === Node.TEXT_NODE) {
|
||||||
document.body.appendChild(document.createTextNode(node.textContent || ""))
|
document.body.appendChild(document.createTextNode(node.textContent || ""))
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
console.log("All resources have been injected and executed in sequence.")
|
||||||
|
}
|
||||||
|
|
||||||
return Promise.all(externalScriptQueue)
|
return executeSequentially().catch((error) => {
|
||||||
.then(() => {
|
|
||||||
console.log("All resources have been injected successfully.")
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error("Error during resource injection:", error)
|
console.error("Error during resource injection:", error)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user