Last active
February 18, 2016 17:29
-
-
Save tobbez/3ae999fdfdea3819c8b7 to your computer and use it in GitHub Desktop.
Proof of concept for migrating localStorage from HTTP to HTTPS
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
<!doctype html> | |
<html> | |
<head> | |
<title>localStorage migration to SSL proof of concept</title> | |
<script type="text/javascript"> | |
function parseHash() { | |
if (document.location.hash === '') return new Map(); | |
var res = new Map(); | |
document.location.hash.substr(1).split('&').forEach(function(e) { | |
var kv = e.split('='); | |
res.set(kv[0], decodeURIComponent(kv[1])); | |
}); | |
return res; | |
} | |
if (document.location.protocol === 'http:') { | |
var data = localStorage.getItem('LocalStorage.pb'); | |
var migrationString = ''; | |
if (data !== null) { | |
localStorage.removeItem('LocalStorage.pb'); | |
migrationString = '#migrate=' + encodeURIComponent(data); | |
} | |
document.location = 'https' + document.location.toString().substr(4) + migrationString; | |
} else { | |
var hashMap = parseHash(); | |
if (hashMap.has('migrate')) { | |
localStorage.setItem('LocalStorage.pb', hashMap.get('migrate')); | |
history.replaceState('', document.title, window.location.pathname + window.location.search); | |
} | |
} | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment