Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing localStorage from a webWorker

Can a WebWorker access the localStorage?

If not why not? Is it problematic from a security stand point?

like image 492
ciochPep Avatar asked May 30 '11 17:05

ciochPep


People also ask

Can I access localStorage from another page?

With the localStorage property, we can save the data into the browser. You can retrieve that data for the same or for any other web page.

How do I access localStorage from service worker?

You cannot access localStorage (and also sessionStorage) from a webworker process, they result will be undefined , this is for security reasons. You need to use postMessage() back to the Worker's originating code, and have that code store the data in localStorage. You should use localStorage.

How do I access localStorage?

It's simple. Just go to the developer tools by pressing F12 , then go to the Application tab. In the Storage section expand Local Storage. After that, you'll see all your browser's local storage there.

Can PWA access local storage?

Note: TWA shares the browser data like cookies and localStorage inside app, so if you are logged in inside a browser then you'll automatically be logged in TWA app as well.


1 Answers

Web workers only have access to the following:

  • XMLHttpRequest
  • Application Cache
  • Create other web workers
  • navigator object
  • location object
  • setTimeout method
  • clearTimeout method
  • setInterval method
  • clearInterval method
  • Performance object (mark,measure,now methods: caniuse?)
  • IndexedDB API (see: caniuse?)
  • importScripts method
  • JSON
  • Worker

The window or parent objects are not accessible from a Web worker therefore you can't access the localStorage.

To communicate between window and the workerglobalscope you may use postMessage() function and onmessage event.

Accessing the DOM and window would not be thread safe, since the child thread would have the same privileges as its parent.

like image 106
filipe Avatar answered Sep 21 '22 05:09

filipe