|
6 | 6 | import AddWindowModal from './lib/startmenu/AddWindowModal.svelte'
|
7 | 7 | import { extractYouTubeVideoId } from './lib/utils'
|
8 | 8 | import DosBox from './lib/applications/DosBox.svelte'
|
| 9 | + import { onMount } from 'svelte' |
9 | 10 |
|
10 | 11 | let addWindowModal = false
|
11 | 12 | let newX = 25
|
|
37 | 38 | if (url.endsWith('.jsdos')) return 'DosBox'
|
38 | 39 | return null
|
39 | 40 | }
|
| 41 | +
|
| 42 | + // Comppreses string to GZIP. Retruns a Promise with Base64 string |
| 43 | + const compress = string => { |
| 44 | + const blobToBase64 = blob => |
| 45 | + new Promise((resolve, _) => { |
| 46 | + const reader = new FileReader() |
| 47 | + reader.onloadend = () => resolve(reader.result.split(',')[1]) |
| 48 | + reader.readAsDataURL(blob) |
| 49 | + }) |
| 50 | + const byteArray = new TextEncoder().encode(string) |
| 51 | + const cs = new CompressionStream('gzip') |
| 52 | + const writer = cs.writable.getWriter() |
| 53 | + writer.write(byteArray) |
| 54 | + writer.close() |
| 55 | + return new Response(cs.readable).blob().then(blobToBase64) |
| 56 | + } |
| 57 | +
|
| 58 | + // Decompresses base64 encoded GZIP string. Retruns a string with original text. |
| 59 | + const decompress = base64string => { |
| 60 | + const bytes = Uint8Array.from(atob(base64string), c => c.charCodeAt(0)) |
| 61 | + const cs = new DecompressionStream('gzip') |
| 62 | + const writer = cs.writable.getWriter() |
| 63 | + writer.write(bytes) |
| 64 | + writer.close() |
| 65 | + return new Response(cs.readable).arrayBuffer().then(function (arrayBuffer) { |
| 66 | + return new TextDecoder().decode(arrayBuffer) |
| 67 | + }) |
| 68 | + } |
| 69 | +
|
| 70 | + onMount(() => { |
| 71 | + const hash = window.location.hash |
| 72 | + if (hash) { |
| 73 | + try { |
| 74 | + decompress(hash.slice(1)).then(value => { |
| 75 | + $windows = JSON.parse(value) |
| 76 | + }) |
| 77 | + } catch (e) { |
| 78 | + console.error('Failed to parse windows from URL', e) |
| 79 | + } |
| 80 | + window.location.hash = '' |
| 81 | + } |
| 82 | + }) |
40 | 83 | </script>
|
41 | 84 |
|
42 | 85 | <main class="desktop-view">
|
|
113 | 156 | $windows = initialWindows()
|
114 | 157 | },
|
115 | 158 | },
|
| 159 | + { |
| 160 | + name: 'Share Desktop', |
| 161 | + click() { |
| 162 | + const url = new URL(window.location.href) |
| 163 | + compress(JSON.stringify($windows)).then(value => { |
| 164 | + url.hash = value |
| 165 | + navigator.clipboard.writeText(url.href) |
| 166 | + }) |
| 167 | + }, |
| 168 | + }, |
116 | 169 | ]}
|
117 | 170 | />
|
118 | 171 | </main>
|
|
0 commit comments