2023-01-14 20:58:52 +00:00
|
|
|
export function useWebShareTarget(listener?: (message: MessageEvent) => void) {
|
2024-02-24 16:46:14 +00:00
|
|
|
if (import.meta.server)
|
2023-01-14 20:58:52 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
onBeforeMount(() => {
|
|
|
|
// PWA must be installed to use share target
|
2023-02-16 09:23:26 +00:00
|
|
|
if (useNuxtApp().$pwa?.isInstalled && 'serviceWorker' in navigator) {
|
2023-01-14 20:58:52 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|