-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport-html.js
More file actions
1 lines (1 loc) · 910 Bytes
/
import-html.js
File metadata and controls
1 lines (1 loc) · 910 Bytes
1
const htmlBody=document.querySelector("body");class HtmlImport extends HTMLElement{constructor(){super();const e=this.dataset.src;try{if(e.split(".").includes("html"))return void this.open(e).then(t=>{console.log(t),404===t.status?this.renderError(e):this.renderHTML(t.html)}).catch(e=>{console.log(e)});let t=document.createElement("div");t.innerHTML=errorMessage(`this file ${e} is not HTML`),htmlBody.appendChild(t)}catch(e){console.log(e)}}renderHTML(e){let t=document.createElement("div");t.innerHTML=e,htmlBody.appendChild(t)}renderError(e){let t=document.createElement("div");t.innerHTML=errorMessage(`${e} is not found`),htmlBody.appendChild(t)}async open(e){try{const t=await fetch(e),r=await t.text(),o=t.status;return await({html:r,status:o})}catch(e){throw new Error(e)}}}const errorMessage=e=>`<code style="color:red">Hey,\n <b> ${e}</b>\n </code>`;customElements.define("import-html",HtmlImport);