function init(root) {
for (const button of root.querySelectorAll('button[data-zeyn-share][hidden]')) button.hidden = false;
}
function toast(message, tone) {
document.dispatchEvent(new CustomEvent('zeyn:run', { detail: { effects: [{ effect: 'toast', message, tone }] } }));
}
async function share(button) {
const url = new URL(button.dataset.url || location.href, location.href).href;
const title = button.dataset.title || document.title;
if (typeof navigator.share === 'function') {
try {
await navigator.share({ url, title });
return;
} catch (err) {
if (err?.name === 'AbortError') return;
}
}
try {
await navigator.clipboard.writeText(url);
toast(button.dataset.copied, 'success');
} catch {
toast(button.dataset.failed, 'danger');
}
}
document.addEventListener('click', (e) => {
const button = e.target instanceof Element ? e.target.closest('button[data-zeyn-share]') : null;
if (button) share(button);
});
document.addEventListener('zeyn:content', (e) => init(e.detail instanceof Element ? e.detail : document));
init(document);