最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to access and read localstorage for an electron application - Stack Overflow

programmeradmin1浏览0评论

I have built an electron app that allows the user to move elements such as divs around the screen, and saves their locations in localstorage as a class. This is so when the page reloads or is reopened after a session, the changes made persist. I can't get electron to access the local storage though, even on a page change, so I was wondering if there is anyway to make electron go and grab the localstorage classes, or if I can make the page itself do it.

I have built an electron app that allows the user to move elements such as divs around the screen, and saves their locations in localstorage as a class. This is so when the page reloads or is reopened after a session, the changes made persist. I can't get electron to access the local storage though, even on a page change, so I was wondering if there is anyway to make electron go and grab the localstorage classes, or if I can make the page itself do it.

Share Improve this question edited May 7, 2018 at 4:20 user890199 asked May 7, 2018 at 1:04 user890199user890199 1931 gold badge2 silver badges10 bronze badges 4
  • what you mean by "localhost access"? – Thaadikkaaran Commented May 7, 2018 at 3:49
  • that would be me meaning to say localstorage, and being distrait and putting the wrong thing. It is meant to be local storage – user890199 Commented May 7, 2018 at 4:21
  • Have you checked the docs? electronjs.org/docs/faq#how-to-share-data-between-web-pages – Sean Commented May 7, 2018 at 4:38
  • Thank you for pointing me to the docs, I will take a look at them – user890199 Commented May 9, 2018 at 0:48
Add a comment  | 

2 Answers 2

Reset to default 28

You can read data from localstorage. Here is what work for me.

mainWindow.webContents
  .executeJavaScript('localStorage.getItem("thekey");', true)
  .then(result => {
    console.log(result);
  });

You gets the entire localStorage and use it as you would do it in a browser. The localStorage variable now is an object with key-values.

mainWindow.webContents
  .executeJavaScript('({...localStorage});', true)
  .then(localStorage => {
    console.log(localStorage);
  });
发布评论

评论列表(0)

  1. 暂无评论