function loadScript(src, callback) {
  // Create a script tag, set its source
  var scriptTag = document.createElement("script"),
    filePath = src;

  // And listen to it
  scriptTag.onload = function (loadEvent) {
    // This function is an event handler of the script tag
    handleEvent(src);
  };

  // Make sure this file actually loads instead of a cached version
  // Add a timestamp onto the URL (i.e. file.js?bust=12345678)
  var cacheBuster = "";

  cacheBuster = "?bust=" + new Date().getTime();

  // Set the type of file and where it can be found
  scriptTag.type = "text/javascript";
  scriptTag.src = filePath + cacheBuster;

  // Finally add it to the <head>
  document.getElementsByTagName("head")[0].appendChild(scriptTag);
}
function handleEvent(src) {
  console.log("The file " + src + " has been loaded. ready.");
  // More code here
}
function getfilein(file) {
  var z, i, elmnt, file, xhttp;
  /* Loop through a collection of all HTML elements: */
  z = document.getElementById("cover");
  //for (i = 0; i < z.length; i++) {
  elmnt = z.innerHTML;
  /*search for elements with a certain atrribute:*/

  z.innerHTML = "test";
  console.log("file in" + file);
  if (file) {
    /* Make an HTTP request using the attribute value as the file name: */
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
      if (this.readyState == 4) {
        if (this.status == 200) {
          z.innerHTML = this.responseText;
        }
        if (this.status == 404) {
          z.innerHTML = "Page not found.";
        }
        /* Remove the attribute, and call this function once more: */
        //elmnt.removeAttribute("cover");
        //includeHTML();
      }
    };
    cacheBuster = "?bust=" + new Date().getTime();
    file = file + cacheBuster;

    xhttp.open("GET", file, true);
    xhttp.send();
    /* Exit the function: */
    return;
    //}
  }
}
function initHTML(title = "title") {
  // Check if <html> exists
  if (!document.documentElement) {
    const html = document.createElement("html");
    html.setAttribute("lang", "en");
    document.appendChild(html);
  }

  // Ensure <head> exists
  let head = document.head;
  if (!head) {
    head = document.createElement("head");
    document.documentElement.appendChild(head);
  }

  // Ensure <body> exists
  let body = document.body;
  if (!body) {
    body = document.createElement("body");
    document.documentElement.appendChild(body);
  }

  // Add meta tags and title to head if not already present
  if (!document.querySelector('meta[charset="UTF-8"]')) {
    const metaCharset = document.createElement("meta");
    metaCharset.setAttribute("charset", "UTF-8");
    head.appendChild(metaCharset);
  }

  if (!document.querySelector('meta[name="viewport"]')) {
    const metaViewport = document.createElement("meta");
    metaViewport.setAttribute("name", "viewport");
    metaViewport.setAttribute(
      "content",
      "width=device-width, initial-scale=1.0",
    );
    head.appendChild(metaViewport);
  }

  if (!document.querySelector("title")) {
    const titleElement = document.createElement("title");
    titleElement.textContent = title;
    head.appendChild(titleElement);
  }

  return { head, body };
}

function makeBodyTag() {
  if (!document.body) {
    var body = document.createElement("body");
    document.documentElement.appendChild(body);
  }
}
function makeDiv(id) {
  var div = document.createElement("div");
  div.id = id;
  console.log("Div created with id: " + id);
  return div;
}
function setDiv(id) {
  var div = document.createElement("div");
  div.id = id;
  console.log("Div created with id: " + id);

  //div.textContent = 'ddd'; // Initial content
  document.body.appendChild(div);
}
function addStyle(stylesText) {
  const styleElement = document.createElement("style");
  styleElement.textContent = stylesText;
  document.head.appendChild(styleElement);
}
