Skip to content

Commit 6d21dc7

Browse files
committed
Add a feature to share the desktop (thanks Sergei)
1 parent df270ca commit 6d21dc7

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/App.svelte

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import AddWindowModal from './lib/startmenu/AddWindowModal.svelte'
77
import { extractYouTubeVideoId } from './lib/utils'
88
import DosBox from './lib/applications/DosBox.svelte'
9+
import { onMount } from 'svelte'
910
1011
let addWindowModal = false
1112
let newX = 25
@@ -37,6 +38,48 @@
3738
if (url.endsWith('.jsdos')) return 'DosBox'
3839
return null
3940
}
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+
})
4083
</script>
4184

4285
<main class="desktop-view">
@@ -113,6 +156,16 @@
113156
$windows = initialWindows()
114157
},
115158
},
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+
},
116169
]}
117170
/>
118171
</main>

0 commit comments

Comments
 (0)