Created
August 16, 2023 21:25
-
-
Save drianoaz/1f7bdd1137aa27085d0e7f460c16fe9f to your computer and use it in GitHub Desktop.
Breakpoint localStorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @see https://stackoverflow.com/questions/49092423/how-to-break-on-localstorage-changes | |
*/ | |
Object.defineProperty(window, 'localStorage', { | |
configurable: true, | |
enumerable: true, | |
value: new Proxy(localStorage, { | |
set: function (ls, prop, value) { | |
console.log(`direct assignment: ${prop} = ${value}`); | |
debugger; | |
ls[prop] = value; | |
return true; | |
}, | |
get: function(ls, prop) { | |
// The only property access we care about is setItem. We pass | |
// anything else back without complaint. But using the proxy | |
// fouls 'this', setting it to this {set: fn(), get: fn()} | |
// object. | |
if (prop !== 'setItem') { | |
if (typeof ls[prop] === 'function') { | |
return ls[prop].bind(ls); | |
} else { | |
return ls[prop]; | |
} | |
} | |
// If you don't care about the key and value set, you can | |
// drop a debugger statement here and just | |
// "return ls[prop].bind(ls);" | |
// Otherwise, return a custom function that does the logging | |
// before calling setItem: | |
return (...args) => { | |
console.log(`setItem(${args.join()}) called`); | |
debugger; | |
ls.setItem.apply(ls, args); | |
}; | |
} | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment