Skip to content
Advertisement

Save loaded HTML file from electron app

I am trying to explore electron and trying to make a simple Mac application. The problem is that I wanted to save the loaded HTML when user closes the application, and upon next start of application the saved html will be loaded.

Now, consider following case: The html which i have loaded initially have some table and items, and during runtime, via javascript I am adding few new rows and values. So the html is modified at runtime. Now when user closes the app, i have to save this html (which is modified). I tried using webContents.savePage but the page which was saved is the HTML which was loaded initially i.e. without runtime changes. How can I do it?

Also I am not able to catch windows.onbeforeunload event, don’t know why. But window.closed is triggerd.

Any help?

Advertisement

Answer

You can create a File or Blob object of document

let file = new File([document.documentElement.outerHTML]
           , "file-" + new Date().getTime() + ".html"
           , {type:"text/html", lastModified:new Date().getTime()});
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement