I use codesnippet plugin to add a block to a page. I want to add a webshare api share button bellow that to share a link $store_url. How can i generate the code for it ?
Asked
Active
Viewed 397 times
1 Answers
0
Please refer to the blog post where the feature was announced. Here is a baseline example:
if (navigator.share) {
navigator.share({
title: 'web.dev',
text: 'Check out web.dev.',
url: 'https://web.dev/',
})
.then(() => console.log('Successful share'))
.catch((error) => console.log('Error sharing', error));
}
For more complex sharing requirements, like sharing files, here's another snippet to get you started:
if (navigator.canShare && navigator.canShare({ files: filesArray })) {
navigator.share({
files: filesArray,
title: 'Vacation Pictures',
text: 'Photos from September 27 to October 14.',
})
.then(() => console.log('Share was successful.'))
.catch((error) => console.log('Sharing failed', error));
} else {
console.log(`Your system doesn't support sharing files.`);
}

DenverCoder9
- 2,024
- 11
- 32
-
how to activate this code with a button. ? – Dazzle Dinil Jun 07 '21 at 19:52
-
You create a button with an event listener attached: `document.querySelector('button').addEventListener('click', () => { /* The function */ });` – DenverCoder9 Jun 08 '21 at 20:04