Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better log messages for canShare #226

Merged
merged 2 commits into from
Nov 22, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member Author

@saschanaz saschanaz Nov 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a problem, as already mentioned in #108 (comment). Something like this would help.

navigator.canShare("files"); // basic feature support
navigator.canShare("files", [files]); // matches canShare({files});

I believe this form of API is discussed somewhere around TPAC 2020 but can't find the log.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#184 was about canShareFiles with file types, but since Blink is now also checking file sizes, I guess receiving a sequence of files is needed?

Copy link
Member Author

@saschanaz saschanaz Nov 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

canShare in Blink seemingly has no check for file types nor file sizes, and share() somehow throws PermissionError instead when it receives an unsupported file or too large file. Maybe to prevent fingerprinting? But then what's the point of canShare with the current form? 👀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hehe, "incremental improvement"?

// 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