From ac0217514f39eb98b49c84112cf11a34c530abb6 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Mon, 22 Nov 2021 13:48:04 +0100 Subject: [PATCH] fix(demos): better log messages for canShare (#226) --- demos/share-files.html | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/demos/share-files.html b/demos/share-files.html index 3ea3951..eef9113 100644 --- a/demos/share-files.html +++ b/demos/share-files.html @@ -118,10 +118,20 @@

textfield.value = ''; } + function checkBasicFileShare() { + // XXX: There is no straightforward API to do this. + // For now, assume that text/plain is supported everywhere. + const txt = new Blob(['Hello, world!'], {type: 'text/plain'}); + // XXX: Blob support? https://github.com/w3c/web-share/issues/181 + const file = new File([txt], "test.txt"); + return navigator.canShare({ files: [file] }); + } + async function testWebShare() { const title_input = document.querySelector('#title'); const text_input = document.querySelector('#text'); const url_input = document.querySelector('#url'); + /** @type {HTMLInputElement} */ const file_input = document.querySelector('#files'); const title = title_input.disabled ? undefined : title_input.value; @@ -130,8 +140,17 @@

const files = file_input.disabled ? undefined : file_input.files; if (files && files.length > 0) { - if (!navigator.canShare || !navigator.canShare({files})) { - logError('Error: Unsupported feature: navigator.canShare()'); + if (!navigator.canShare) { + logError('Warning: canShare is not supported. File sharing may not be supported at all.'); + } else if (!checkBasicFileShare()) { + logError('Error: File sharing is not supported in this browser.'); + setShareButtonsEnabled(true); + return; + } else if (!navigator.canShare({files})) { + logError('Error: share() does not support the given files'); + for (const file of files) { + logError(`File info: name - ${file.name}, size ${file.size}, type ${file.type}`); + } setShareButtonsEnabled(true); return; }