Skip to content

Commit

Permalink
fix(demos): better log messages for canShare (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanaz authored Nov 22, 2021
1 parent ac3022d commit ac02175
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions demos/share-files.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,20 @@ <h1>
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;
Expand All @@ -130,8 +140,17 @@ <h1>
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;
}
Expand Down

0 comments on commit ac02175

Please sign in to comment.