forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration-helper.html
58 lines (51 loc) · 1.73 KB
/
migration-helper.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>Migration IFrame</title>
</head>
<body>
<p>This is a hidden page for migrating old Sprig games to the new Sprig account system. You probably won't find anything here :)</p>
<form method='post' id='form'>
<input type='hidden' id='puzzle-lab' name='puzzle-lab' value=''>
</form>
<script>
const urlAllowed = (url) => {
const parsed = new URL(url)
return [ 'localhost', '127.0.0.1' ].includes(parsed.hostname)
|| /^sprig(-beta)?(-[a-zA-Z0-9]+)?\.hackclub\.dev$/.test(parsed.hostname)
|| /^sprig-git-[a-zA-Z0-9\-]+\.hackclub\.dev$/.test(parsed.hostname)
|| parsed.hostname.endsWith('.hackclub.com')
}
const sendToParent = async () => {
if (!urlAllowed(document.referrer)) throw new Error('Disallowed referrer')
try {
if (typeof document.requestStorageAccess === 'function')
await document.requestStorageAccess()
} catch (error) {
if (error) console.warn(error)
window.parent.postMessage({
sprig: true,
puzzleLab: '',
storageAccessDenied: true
}, '*')
return
}
window.parent.postMessage({
sprig: true,
puzzleLab: window.localStorage.getItem('puzzle-lab') || '',
storageAccessDenied: false
}, '*')
}
const params = new URLSearchParams(window.location.search)
if (params.has('redirect')) {
if (!urlAllowed(params.get('redirect'))) throw new Error('Disallowed redirect URL')
document.getElementById('form').action = params.get('redirect')
document.getElementById('puzzle-lab').value = window.localStorage.getItem('puzzle-lab') || ''
document.getElementById('form').submit()
} else {
sendToParent()
}
</script>
</body>
</html>