blogob
blogob

Reputation: 530

How can I save an html file and remove script and unwanted tag when dowloading?

I have an file with lots of tags with contenteditable="true" attribute. I also added a lot of jquery/js inside.

What I'm trying to achieve is:

  1. Let user modify the content --> ok with contenteditable attribute
  2. Let user download the modified file --> ok with the snippet below
  3. When clicking on download button, remove all content between "Delete" and "Delete End" comments, and also remove all "contenteditable="true" attributes --> not ok

I also found this download snippet which is drastically shorter than the one I use in the following snippet, but don't know which is better, safer?

<a onclick="this.href='data:text/html;charset=UTF-8,'+encodeURIComponent(document.documentElement.outerHTML)" href="#" download="index.html">Download</a>

Here is an example of my file:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <!--Delete Start-->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  <!--Delete End-->
  <title></title>
</head>

<body style="margin: 0; padding: 0 !important;background-color: #F2F2F2;">
  <div contenteditable="true">
    <p style="margin: 0 0 8px;">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  </div>

  <!--Delete Start-->
  <div>
    <input type="button" id="add" value="add content">
    <input type="button" id="del" value="remove content">
  </div>
  <!--Delete End-->


  <!--Delete Start-->
  <div>
    <a href="#" id="donwload-link" onClick="myFunction()">download html content</a>
  </div>
  <!--Delete End-->


  <!--Delete Start-->
  <script>
    function myFunction() {
      var content = document.documentElement.innerHTML;
      download(content, "index", "html")
    }

    function download(content, fileName, fileType) {
      var link = document.getElementById("donwload-link");
      var file = new Blob([content], {
        type: "html"
      });
      var donwloadFile = fileName + "." + fileType;
      link.href = URL.createObjectURL(file);
      link.download = donwloadFile
    }
  </script>
  <!--Delete End-->
</body>

</html>

Upvotes: 1

Views: 1056

Answers (1)

phentnil
phentnil

Reputation: 2279

Set the class attribute to something like delete and use querySelectorAll(".delete, script"). Then loop through these elements and use element.remove() in your clean() function in addition to removing the contentEditable from the other elements.

body {
  margin: 0;
  padding: 0;
  background-color: #f2f2f2;
}

p {
  margin: 0 0 8px;
}
<!--Delete Start-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<!--Delete End-->

<p contenteditable="true">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
  in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

<!--Delete Start-->
<div class="delete">
  <input type="button" id="add" value="add content">
  <input type="button" id="del" value="remove content">
</div>
<!--Delete End-->

<!--Delete Start-->
<div class="delete">
  <a href="#" id="download-link" onClick="myFunction()">download html content</a>
</div>
<!--Delete End-->

<!--Delete Start-->
<script>
  function myFunction() {
    clean();
    var content = document.documentElement.innerHTML;
    download(content, "index", "html");
  }

  function clean() {
    var contentToDelete = document.querySelectorAll(".delete, script");
    var editableContent = document.querySelectorAll("[contenteditable=true]");
    for (var i = 0; i < editableContent.length; i++) {
      editableContent[i].removeAttribute('contenteditable');
    }
    contentToDelete.forEach((element) => element.remove());
  }

  function download(content, fileName, fileType) {
    var link = document.createElement("a");
    var file = new Blob([content], {
      type: "html"
    });
    var downloadFile = fileName + "." + fileType;
    link.href = URL.createObjectURL(file);
    link.download = downloadFile;
    link.click();
  }
</script>
<!--Delete End-->

Upvotes: 2

Related Questions