javascriptelectronweb-content

How to save web contents to another html file


I'm new to JavaScript and Electron Projects. I have a small task to Run a Webpage in which I have "download" button, if I hit the button I have to get all contents and sources code to be downloaded for the current page. Here my Sample work :

browser.js

onload = function() {
var webview = document.querySelector('webview');
doLayout();

document.querySelector('#back').onclick = function() {
webview.goBack();
};

document.querySelector('#download').onclick = function() {

var urlStr = webview.getURL()

alert(urlStr)

// alert(webview.getWebContents());
  };
}

Currently I am able to get Url in Alert View, but I'm not able to get webpage contents

Note: Please give solution only in JavaScript not in jQuery


Solution

  • finally this simple code helped me to download the html file

    var htmlContent = [""];
    var bl = new Blob(htmlContent, {type: "text/html"});
    var a = document.createElement("a");
    a.href = urlStr;
    a.download = "new.html";
    a.hidden = true;
    document.body.appendChild(a);
    a.innerHTML = "something random - nobody will see this, it doesn't matter  what you put here";
    a.click()