fix migrate using url hash

This commit is contained in:
WEBXOSS 2017-03-07 00:48:25 +08:00
parent 47ea4fc3fc
commit 5ecab27c95
2 changed files with 51 additions and 58 deletions

View file

@ -11,6 +11,39 @@
<script>
var global = window;
if (window.location != window.parent.location) window.parent.location = window.location;
// recieve migrated localStorage
(function () {
debugger
var storageJson = location.href.split('#storage=')[1]
if (storageJson && localStorage.getItem('migrated') !== '2') {
try {
var storage = JSON.parse(storageJson)
for (var key in storage) {
if (localStorage.getItem(key) !== null) {
// confilct
if (key.indexOf('deck_file_') !== 0) {
continue
}
key = key + '.old'
}
localStorage.setItem(key, storage[key])
}
// reset deck file names
var names = []
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i)
if (key.indexOf('deck_file_') === 0) {
names.push(key.replace('deck_file_', ''))
}
}
location.hash = ''
localStorage.setItem('deck_filenames', JSON.stringify(names))
localStorage.setItem('migrated', 2)
} catch (e) {
console.warn('Failed to migrate localStorage.')
}
}
})()
</script>
<link rel="stylesheet" type="text/css" href="./css.css" />
</head>

View file

@ -7,6 +7,19 @@
<iframe id="iframe" style="display: none;" src="about:blank"></iframe>
<script>
(function () {
if (location.protocol === 'https:') {
return
}
// migrated by the old way, buggy in safari
if (localStorage.getItem('migrated') === 'true') {
if (navigator.userAgent.indexOf('safari') === -1) {
return location.replace(location.href.replace(/^http:/, 'https:'))
}
}
// migrated by the new way
if (localStorage.getItem('migrated') === '2') {
return location.replace(location.href.replace(/^http:/, 'https:'))
}
function getStorage() {
var storage = {}
for (var i = 0; i < localStorage.length; i++) {
@ -15,64 +28,11 @@
}
return storage
}
var receive = location.protocol === 'https:'
var httpOrigin = location.origin.replace(/^https:/, 'http:')
var httpsOrigin = location.origin.replace(/^http:/, 'https:')
var iframe = document.getElementById('iframe')
if (receive) {
handleReciever()
} else {
handleSender()
}
function handleReciever() {
if (localStorage.getItem('migrated')) {
// already migrated, send stored signal
return window.parent.postMessage('stored', httpOrigin)
}
if (window.parent === window) {
return console.error('Not in iframe.')
}
// recieve storage
window.addEventListener('message', function (event) {
var origin = event.origin || event.originalEvent.origin;
if (origin !== httpOrigin) {
return console.warn(event)
}
var storage = JSON.parse(event.data)
for (var key in storage) {
localStorage.setItem(key, storage[key])
}
// send stored signal
window.parent.postMessage('stored', httpOrigin)
}, false)
// send ready signal
window.parent.postMessage('ready', httpOrigin)
}
function handleSender() {
if (localStorage.getItem('migrated')) {
// already migrated, redirect to https
return location.href = location.href.replace(/^http:/, 'https:')
}
window.addEventListener('message', function (event) {
var origin = event.origin || event.originalEvent.origin;
if (origin !== httpsOrigin) {
return console.warn(event)
}
// get ready signal, send storage
if (event.data === 'ready') {
iframe.contentWindow.postMessage(JSON.stringify(getStorage()), httpsOrigin)
}
// get stored signal, done
if (event.data === 'stored') {
localStorage.setItem('migrated', true)
return location.href = location.href.replace(/^http:/, 'https:')
}
}, false)
iframe.src = 'https://' + location.host + '/migrate.html'
}
var storage = getStorage()
localStorage.setItem('migrated', 2)
// pass localStorage via url hash, which is ignored in network request
var url = location.href.replace(/^http:/, 'https:') + '#storage=' + JSON.stringify(storage)
return location.replace(url)
})()
</script>
</body>