The Web Share API in JavaScript does not support sharing text along with files and URLs

49 Views Asked by At

I am using the Web Share API in JavaScript. When I set all four properties - title, text, URL, and files, it only shares the URL and files, excluding the text. I attempted sharing on WhatsApp, Twitter, Telegram, and Facebook. In all cases, it sends the URL and files except on Facebook, where it only sends the files. I tested this on Android Chrome. How can I fix this issue?

const shareButton = document.getElementById('shareButton');
shareButton.addEventListener('click', async () => {
    try {
        const blob = await fetch(base64Image).then(response => response.blob());
        const file = new File([blob], 'image.png', { type: 'image/png' });
        // Check if the Web Share API is supported by the browser
        if (navigator.canShare && navigator.canShare(file)){
                await navigator.share({
                    title: 'Share Image',
                    text: 'Check out this generated image! http://www.google.com',
                    url: 'http://www.google.com',
                    files: [file],
                });
          } else {
             // Web Share API not supported, provide fallback
             alert('Sorry, your browser does not support the Share API or Your system does not support sharing these files.');
          }
    } catch (error) {
        console.error('Error sharing image:', error);
    }
});

I expect that this API shares at least text, URL, and one file.

0

There are 0 best solutions below