mirror of
https://github.com/webxoss/webxoss-client.git
synced 2025-01-18 10:25:59 +01:00
79 lines
No EOL
2.3 KiB
HTML
79 lines
No EOL
2.3 KiB
HTML
<!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> |