Search code examples
javascriptlocal-storageweb-storage

Intercept localStorage.getItem()


Is there any way to intercept calls to localStorage? Or maybe some handlers exists?

If I have somewhere in code script like localStorage.getItem() I need to know that storage going to get some item, and I want to prevent this, and do something I need.

I want to write a framework for a mobile web developers. I want them to use localstorage calls in usual way. My framework will intercept this calls and will put data not to W3C storage, but to native device storage. Is that possible? Thanks.


Solution

  • You can use prototypes. Try something like that:

     Storage.prototype._setItem = Storage.prototype.setItem;
            Storage.prototype.setItem = function (key, value)
            {
                alert("works");
            }
    
        localStorage.setItem("A", "A");
    

    If you'll need farther clarification feel free to ask.