1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-19 07:19:58 +00:00
elk/composables/web-share-target.ts
Horváth Bálint bede92404b
feat: add support for the Web Share Target API (#1100)
Co-authored-by: userquin <userquin@gmail.com>
2023-01-14 20:58:52 +00:00

25 lines
873 B
TypeScript

export function useWebShareTarget(listener?: (message: MessageEvent) => void) {
if (process.server)
return
onBeforeMount(() => {
// PWA must be installed to use share target
if (useNuxtApp().$pwa.isInstalled && 'serviceWorker' in navigator) {
if (listener)
navigator.serviceWorker.addEventListener('message', listener)
navigator.serviceWorker.getRegistration()
.then((registration) => {
if (registration && registration.active) {
// we need to signal the service worker that we are ready to receive data
registration.active.postMessage({ action: 'ready-to-receive' })
}
})
.catch(err => console.error('Could not get registration', err))
if (listener)
onBeforeUnmount(() => navigator.serviceWorker.removeEventListener('message', listener))
}
})
}