There are several possibilities to achieve what you want. But remember that none of them should be used in a production environment.
The first parameter determines whether the setItem method was called by the sessionStorage or localStorage . You can write like this:
var _setItem = Storage.prototype.setItem; Storage.prototype.setItem = function(key, value) { if (this === window.localStorage) { // do what you want if setItem is called on localStorage } else { // fallback to default action _setItem.apply(this, arguments); } }
Second, replaces the prototype of the sessionStorage or localStorage . It might look like this:
localStorage.__proto__ = Object.create(Storage.prototype); localStorage.__proto__.setItem = function() {
Please note that I used the __proto__ pseudo property, which is non-standard, but available in Chrome and Firefox. (I donβt know about Opera, Safari and others). However, as you can see, this can be useful during development.
source share