improvements to page-script

This commit is contained in:
sbwalker 2024-12-16 12:44:07 -05:00
parent dedfbba27a
commit d991b57d08

View File

@ -45,6 +45,7 @@ async function initializePageScript(pageScriptInfo) {
console.error("Failed to load script: ${pageScriptInfo.src}", error); console.error("Failed to load script: ${pageScriptInfo.src}", error);
} }
} }
removePageScript(pageScriptInfo);
} }
function onEnhancedLoad() { function onEnhancedLoad() {
@ -72,16 +73,18 @@ function onEnhancedLoad() {
} }
} }
} }
for (const [key, pageScriptInfo] of pageScriptInfoBySrc) {
removePageScript(pageScriptInfo);
}
} }
function injectScript(pageScriptInfo) { function injectScript(pageScriptInfo) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var pageScript;
var script = document.createElement("script"); var script = document.createElement("script");
script.async = false; script.async = false;
if (pageScriptInfo.src !== "") { if (pageScriptInfo.src !== "") {
pageScript = document.querySelector("page-script[src=\"" + pageScriptInfo.src + "\"]");
script.src = pageScriptInfo.src; script.src = pageScriptInfo.src;
if (pageScriptInfo.type !== "") { if (pageScriptInfo.type !== "") {
script.type = pageScriptInfo.type; script.type = pageScriptInfo.type;
@ -93,7 +96,6 @@ function injectScript(pageScriptInfo) {
script.crossOrigin = pageScriptInfo.crossorigin; script.crossOrigin = pageScriptInfo.crossorigin;
} }
} else { } else {
pageScript = document.querySelector("page-script[content=\"" + CSS.escape(pageScriptInfo.content) + "\"]");
script.innerHTML = pageScriptInfo.content; script.innerHTML = pageScriptInfo.content;
} }
@ -101,25 +103,27 @@ function injectScript(pageScriptInfo) {
script.onerror = (error) => reject(error); script.onerror = (error) => reject(error);
// add script to page // add script to page
if (pageScriptInfo.location === "head") {
document.head.appendChild(script); document.head.appendChild(script);
});
}
function removePageScript(pageScriptInfo) {
var pageScript;
if (pageScriptInfo.src !== "") {
pageScript = document.querySelector("page-script[src=\"" + pageScriptInfo.src + "\"]");
} else { } else {
document.body.appendChild(script); pageScript = document.querySelector("page-script[content=\"" + CSS.escape(pageScriptInfo.content) + "\"]");
// note this throws an exception when page-script is on a page which has interactive components (ie Counter.razor)
// Error: Uncaught (in promise) TypeError: can't access property "attributes" of null
// this seems to be related to Blazor-Server-Component-State which is also injected at the end of the body
} }
// remove page-script element from page if (pageScript) {
if (pageScript !== null) {
pageScript.remove(); pageScript.remove();
} }
});
} }
export function afterWebStarted(blazor) { export function afterWebStarted(blazor) {
customElements.define('page-script', class extends HTMLElement { customElements.define('page-script', class extends HTMLElement {
static observedAttributes = ['src', 'type', 'integrity', 'crossorigin', 'content', 'location', 'reload']; static observedAttributes = ['src', 'type', 'integrity', 'crossorigin', 'content', 'reload'];
constructor() { constructor() {
super(); super();
@ -129,7 +133,6 @@ export function afterWebStarted(blazor) {
this.integrity = ""; this.integrity = "";
this.crossorigin = ""; this.crossorigin = "";
this.content = ""; this.content = "";
this.location = "head";
this.reload = false; this.reload = false;
} }
@ -150,18 +153,16 @@ export function afterWebStarted(blazor) {
case "content": case "content":
this.content = newValue; this.content = newValue;
break; break;
case "location":
this.location = newValue;
break;
case "reload": case "reload":
this.reload = newValue; this.reload = newValue;
break; break;
} }
}
connectedCallback() { // if last attribute for element has been processed
if (this.attributes[this.attributes.length - 1].name === name) {
registerPageScriptElement(this); registerPageScriptElement(this);
} }
}
disconnectedCallback() { disconnectedCallback() {
unregisterPageScriptElement(this); unregisterPageScriptElement(this);