Skip to content

Instantly share code, notes, and snippets.

@umanghome
Created October 17, 2020 21:38
Show Gist options
  • Save umanghome/3dcf5633bc709006624219c6e7aad7f5 to your computer and use it in GitHub Desktop.
Save umanghome/3dcf5633bc709006624219c6e7aad7f5 to your computer and use it in GitHub Desktop.
Basic WebShare API Code
function showElement(element) {
element.classList.remove('hidden');
}
function sharePage() {
const share = {
text: document.querySelector('title').innerText,
url: window.location.href,
};
navigator.share(share);
}
function addShareListener(element) {
element.addEventListener('click', sharePage);
}
if (navigator.share) {
Array.from(document.querySelectorAll('.display-webshare-only')).forEach(showElement);
Array.from(document.querySelectorAll('.webshare-trigger')).forEach(addShareListener);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment