Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access and read localstorage for an electron application

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.

like image 415
user890199 Avatar asked May 07 '18 01:05

user890199


People also ask

Does localStorage work on electron?

localStorage Web API. This package is compliant and works without any errors as of Electron v8. 3.0 and it is regularly updated.

Can localStorage be accessed?

localStorage is quite insecure as it has no form of data protection and can be accessed by any code on your web page.

Can localStorage be hacked?

As local storage was never intended to be secure, there is no data protection and any JavaScript on the website can access it. Hackers can exploit the existing XSS vulnerability on the website like the following screenshot when the user browses to https://set-localstorage.herokuapp.com/xss-injected-page.html.


2 Answers

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

mainWindow.webContents
  .executeJavaScript('localStorage.getItem("thekey");', true)
  .then(result => {
    console.log(result);
  });
like image 89
Dody Rachmat Wicaksono Avatar answered Nov 14 '22 23:11

Dody Rachmat Wicaksono


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);
  });
like image 26
MetaTron Avatar answered Nov 14 '22 22:11

MetaTron