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

Time:03-11

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

What I'trying to achive 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 drasticaly 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-->
  <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-->
  <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-->
     <div>    
        <input type="button" id="add" value="add content">
        <input type="button" id="del" value="remove content">
      </td>
      <!--Delete-->
    

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

  <!--Delete-->
  <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-->
</body>
</html>

Edit

Thanks to the answer given below by Phentnil, I finaly also found a solution that work with a regex to delete all content between my two comment "Delete". The only problem is that it also delete the html et doctype tag..but it's working.

function myFunction() {
  clean();
  var content = document.documentElement.innerHTML;
  var searchRegExp = /\<\!\-\-Delete\-\-\>((.|[\n|\r|\r\n])*?)\<\!\-\-Delete\-\-\>[\n|\r|\r\n]?(\s )?/g;
  var replaceWith = ' ';
  var result = content.replace(searchRegExp,replaceWith);
  console.log(result);
  download(result, "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();
}

CodePudding user response:

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 >
  <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="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-->

  • Related