0

I am developing a Chrome app that uses some NPM's. When a dependency in my app uses window.localStoarge is there a way that I can map that to chrome.storage.local?

I want to override window.localstorage with something that works with chrome.storage.local instead. This way I do not have to replace code in dependencies that are using local storage.

Is there an npm or something that can do this?

Tabbyofjudah
  • 1,973
  • 3
  • 17
  • 29
  • I think your inquiry is quite similar to [this one](http://stackoverflow.com/questions/13612643/is-it-possible-to-override-local-storage-and-session-storage-separately-in-html5) (though asking on other HTML5 methods). – adjuremods Mar 10 '16 at 08:03

1 Answers1

1

First problem is that chrome.storage APIs are asynchronous and localStorage is sync. It is theoretically possible to create localStorage mock backed by the chrome.storage, but it will break in many cases.

Second problem is that chrome.storage operates on objects, when localStorage only allows strings. So if you'll have some kind of code which will rely on localStorage mode, you will have only store strings in chrome.storage.local, otherwise you will have very weird bugs.

Last problem is that you can't reassign window.localStorage variable in chrome apps, so the only way is to wrap the code into self executing closure, and provide window and localStorage mocks as closure variables, e.g:

(function(window,localStorage){
    //some code relying on localStorage here
})(windowObjectMock,windowObjectMock.localStorage);

It is easier and more error-prone to rewrite the external code to use chrome.storage.local rather than trying to provide localStorage implementation backed by chrome.storage.local.

jusio
  • 9,850
  • 1
  • 42
  • 57