Guarantee execution order of <script> elements (#1574)
This commit is contained in:
parent
35c8449ab2
commit
36e20fd110
@ -59,7 +59,16 @@ export function _queueAction(action: Callback): void {
|
|||||||
_updatingQueue = _updatingQueue.then(action);
|
_updatingQueue = _updatingQueue.then(action);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setInnerHTML(element: Element, html: string): void {
|
function replaceScriptPromise(script: HTMLScriptElement): Promise<void> {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const newScript = script.cloneNode(true);
|
||||||
|
newScript.addEventListener("load", () => resolve());
|
||||||
|
newScript.addEventListener("error", () => resolve());
|
||||||
|
script.replaceWith(newScript);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function setInnerHTML(element: Element, html: string): Promise<void> {
|
||||||
for (const oldVideo of element.getElementsByTagName("video")) {
|
for (const oldVideo of element.getElementsByTagName("video")) {
|
||||||
oldVideo.pause();
|
oldVideo.pause();
|
||||||
|
|
||||||
@ -73,14 +82,11 @@ function setInnerHTML(element: Element, html: string): void {
|
|||||||
element.innerHTML = html;
|
element.innerHTML = html;
|
||||||
|
|
||||||
for (const oldScript of element.getElementsByTagName("script")) {
|
for (const oldScript of element.getElementsByTagName("script")) {
|
||||||
const newScript = document.createElement("script");
|
if (oldScript.src) {
|
||||||
|
await replaceScriptPromise(oldScript);
|
||||||
for (const attribute of oldScript.attributes) {
|
} else {
|
||||||
newScript.setAttribute(attribute.name, attribute.value);
|
oldScript.replaceWith(oldScript.cloneNode(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
newScript.appendChild(document.createTextNode(oldScript.innerHTML));
|
|
||||||
oldScript.parentNode!.replaceChild(newScript, oldScript);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user