Skip to content

Instantly share code, notes, and snippets.

@tobbez
Last active February 18, 2016 17:29
Show Gist options
  • Save tobbez/3ae999fdfdea3819c8b7 to your computer and use it in GitHub Desktop.
Save tobbez/3ae999fdfdea3819c8b7 to your computer and use it in GitHub Desktop.
Proof of concept for migrating localStorage from HTTP to HTTPS
<!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