25 lines
No EOL
692 B
JavaScript
25 lines
No EOL
692 B
JavaScript
class HTMLImportElement extends HTMLTemplateElement {
|
|
|
|
get src(){
|
|
return new URL(this.getAttribute("src"), this.baseURI).toString();
|
|
}
|
|
|
|
connectedCallback(){
|
|
this.load()
|
|
}
|
|
|
|
async load(){
|
|
let res = await fetch(this.src);
|
|
if(!res.ok){
|
|
console.error(`Server responded with code ${res.status} ${res.statusText}`)
|
|
return
|
|
}
|
|
|
|
let htmlStr = await res.text()
|
|
let html = new DOMParser().parseFromString(htmlStr, "text/html");
|
|
|
|
this.content.replaceChildren(...Array.from(html.querySelector("body").childNodes))
|
|
}
|
|
}
|
|
|
|
customElements.define("camp-html", HTMLImportElement, {extends: "template"}) |