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
2 Answers
Reset to default 28You 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);
});