// web_share_api_test.js // RJM Programming // July, 2022 // Thanks to https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share // Help out web_share_api_test.html perhaps var shareData = { title: 'IT Blog', text: 'RJM Programming Blog', url: 'https://www.rjmprogramming.com.au/ITblog/' }; if ((document.head.innerHTML + document.body.innerHTML).indexOf('web_share_api_test.js?populate=') != -1 && (!document.getElementById('files') || !document.getElementById('output') || !document.getElementById('shareurl') || !document.getElementById('share'))) { if (navigator.canShare) { shareData = { title: 'Quarter Hour Timer', text: 'Quarter Hour Timer via MAMP local web server', url: 'https://www.rjmprogramming.com.au/HTMLCSS/quarter_hour_timer.html#Quarter Hour Timer via MAMP local web server#Quarter Hour Timer' }; document.body.innerHTML+="

"; setTimeout(eventuallyshow, 20000); } } function eventuallyshow() { document.getElementById('mydivshare').style.display='block'; } async function atclick() { const files = document.getElementById('files').files; if (files.length === 0) { shareurl(); document.getElementById('output').textContent = 'No files selected.'; return; } // feature detecting navigator.canShare() also implies // the same for the navigator.share() if (!navigator.canShare) { //if (document.URL.indexOf('localhost') != -1) { alert('Can not share'); } document.getElementById('output').textContent = `Your browser doesn't support the Web Share API.`; return; //} else { //if (document.URL.indexOf('localhost') != -1) { alert('Can Share'); } } if (navigator.canShare({ files })) { try { console.log('Can share'); await navigator.share({ files, title: 'Timekeeping screenshots or media or documents', text: 'Timekeeping screenshots perhaps?! Take a look at media or documents below' + String.fromCharCode(10) + String.fromCharCode(10) }); document.getElementById('output').textContent = 'Shared!'; } catch (error) { document.getElementById('output').textContent = `Error: ${error.message}`; } } else { //if (document.URL.indexOf('localhost') != -1) { alert('Cannot share'); } document.getElementById('output').textContent = `Your system doesn't support sharing these files.`; } } async function atthestart() { if (document.getElementById('files') && document.getElementById('output') && document.getElementById('shareurl') && document.getElementById('share')) { //if (document.URL.indexOf('localhost') != -1) { alert('here'); } const input = document.getElementById('files'); const output = document.getElementById('output'); document.getElementById('share').addEventListener('click', async () => { const files = input.files; if (files.length === 0) { shareurl(); output.textContent = 'No files selected.'; return; } // feature detecting navigator.canShare() also implies // the same for the navigator.share() if (!navigator.canShare) { //if (document.URL.indexOf('localhost') != -1) { alert('Can not share'); } output.textContent = `Your browser doesn't support the Web Share API.`; return; //} else { //if (document.URL.indexOf('localhost') != -1) { alert('Can Share'); } } if (navigator.canShare({ files })) { try { console.log('Can share'); await navigator.share({ files, title: 'Media or documents', text: 'Take a look at media or documents below' + String.fromCharCode(10) + String.fromCharCode(10) }); output.textContent = 'Shared!'; } catch (error) { output.textContent = `Error: ${error.message}`; } } else { if (document.URL.indexOf('localhost') != -1) { alert('Cannot share'); } output.textContent = `Your system doesn't support sharing these files.`; } }); } else { setTimeout(atthestart, 1000); } } async function shareurl() { if (document.getElementById('shareurl').value == '') { document.getElementById('shareurl').value=document.getElementById('shareurl').placeholder; } if (document.getElementById('shareurl').value.trim() != '') { var hs=document.getElementById('shareurl').value.split('#'); shareData.url=hs[0]; shareData.text='Url'; shareData.title='Url'; if (hs.length > 1) { shareData.text=decodeURIComponent(hs[1]); shareData.title=decodeURIComponent(hs[1]); } if (hs.length > 2) { shareData.title=decodeURIComponent(hs[2]); } } try { await navigator.share(shareData); document.getElementById('output').textContent = shareData.text + ' shared successfully'; } catch(err) { document.getElementById('output').textContent = 'Error: ' + err; } } setTimeout(atthestart, 1000);