-
-
Save dominwong4/5104eb72fb849a3b3416a373fd1f512b to your computer and use it in GitHub Desktop.
Javascript overriding LocalStorage and SessionStorage for hiding data
This file contains hidden or 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
//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