简体   繁体   English

保存从电子应用程序加载的 HTML 文件

[英]Save loaded HTML file from electron app

I am trying to explore electron and trying to make a simple Mac application.我正在尝试探索电子并尝试制作一个简单的 Mac 应用程序。 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.问题是我想在用户关闭应用程序时保存加载的 HTML,并在下次启动应用程序时加载保存的 html。

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.现在,考虑以下情况:我最初加载的 html 有一些表和项目,在运行时,我通过 javascript 添加了一些新行和值。 So the html is modified at runtime.所以html是在运行时修改的。 Now when user closes the app, i have to save this html (which is modified).现在,当用户关闭应用程序时,我必须保存此 html(已修改)。 I tried using webContents.savePage but the page which was saved is the HTML which was loaded initially ie without runtime changes.我尝试使用 webContents.savePage 但保存的页面是最初加载的 HTML,即没有运行时更改。 How can I do it?我该怎么做?

Also I am not able to catch windows.onbeforeunload event, don't know why.我也无法捕获 windows.onbeforeunload 事件,不知道为什么。 But window.closed is triggerd.但是 window.closed 被触发了。

Any help?有什么帮助吗?

You can create a File or Blob object of document 您可以创建documentFileBlob对象

let file = new File([document.documentElement.outerHTML]
           , "file-" + new Date().getTime() + ".html"
           , {type:"text/html", lastModified:new Date().getTime()});

You can use the 'close' Event from the BrowserWindow to prevent closing the window in the main process.您可以使用 BrowserWindow 中的“关闭”事件来防止在主进程中关闭窗口。

https://www.electronjs.org/docs/latest/api/browser-window#event-close https://www.electronjs.org/docs/latest/api/browser-window#event-close

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM