Reputation: 530
I have an html 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:
contenteditable
attribute"contenteditable="true"
attributes --> not okI 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
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