Skip to content

Instantly share code, notes, and snippets.

@dominwong4
Created February 20, 2021 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominwong4/5104eb72fb849a3b3416a373fd1f512b to your computer and use it in GitHub Desktop.
Save dominwong4/5104eb72fb849a3b3416a373fd1f512b to your computer and use it in GitHub Desktop.
Javascript overriding LocalStorage and SessionStorage for hiding data
//For React, just put this into anyway inside the react project
const _setItem = Storage.prototype.setItem;
const _getItem = Storage.prototype.getItem;
Storage.prototype.setItem = function(key, value) {
if (this === sessionStorage) { //example of specific Storage type
_setItem.call(this, key, btoa(encodeURIComponent(value)));//encodingURI is for language other than Latin
} else {
_setItem.call(this, arguments[0], arguments[1]);
}
};
Storage.prototype.getItem = function(key) {
if (this === sessionStorage) {
//preventing error if xxxStorage.getItem == null. because atob(null) will return error
return _getItem.call(this, key) ? decodeURIComponent(atob(_getItem.call(this, key))) : _getItem.call(this, key);
}
return _getItem.call(this, key);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment