add migrate script

This commit is contained in:
WEBXOSS 2017-03-04 00:04:06 +08:00
parent 8ff00e83cf
commit 0210770dc0

79
migrate.html Normal file
View file

@ -0,0 +1,79 @@
<!DOCTYPE html>
<html manifest="webxoss.appcache">
<head>
<title>Migrate WEBXOSS data</title>
</head>
<body>
<iframe id="iframe" style="display: none;" src="about:blank"></iframe>
<script>
(function () {
function getStorage() {
var storage = {}
for (var i = 0; i < localStorage.length; i++) {
var key = localStorage.key(i)
storage[key] = localStorage.getItem(key)
}
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.protocol = '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.protocol = 'https:'
}
}, false)
iframe.src = 'https://' + location.host + '/migrate.html'
}
})()
</script>
</body>
</html>