I have a webpage that gets most of its content via API calls to a cloud database solution. The HTML page is fairly barebones but gets much more data injected through a number of JS/JQuery commands, etc.
The resulting page represents a "Quote" which I'd like to save back into the cloud database for reference purposes.
I can get the current state of the page and store it in a variable by using the following command:
var AVMI_thisPage = document.getElementsByTagName('html')[0].outerHTML;
I now need to remove any <script> tags from the variable so that any reimport of the HTML back to the cloud database doesn't contain any JS that is likely to mess with the page again when someone opens it for reference.
I should be able to push the string back to the database but I need to get rid of any <script>.
I've tried JQuery but this seems to kill the HTML, HEAD, and BODY tags.
To be honest, I wasn't expecting the code below to work anyway... but tried it.
E.g.
var AVMI_thisPage = document.getElementsByTagName('html')[0].outerHTML;
var AVMI_tree = $("<div>" + AVMI_thisPage + "</div>");
AVMI_tree.find('script').remove();
AVMI_thisPage = AVMI_tree.html();
Any ideas?
UPDATED - FINAL CODE (including BASE64 encoding and upload)
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
var htmlPage = $("html");
$("script", htmlPage).remove();
AVMI_thisPage = htmlPage.html();
AVMI_thisPageB64 = b64EncodeUnicode(AVMI_thisPage);
var req = "";
req += "<qdbapi>";
req += "<rid>" + AVMI_quoteRID + "</rid>";
req += "<field fid='171' filename='Hardcopy of Quote.html'>"+ AVMI_thisPageB64 + "</field>";
req += "</qdbapi>";
$.ajax({
type: "POST",
contentType: "text/xml",
dataType: "xml",
processData: false,
url: "https://xxxx.xxxxxxxx.com/db/" + AVMI_Q_DBID + "?act=API_UploadFile",
data: req
})
.then(function() {
alert("A copy of this quote has been saved into the 'Hardcopy Attachment' field.");
window.close();
});