Update error handling for navigator.clipboard (#1333)

This commit is contained in:
toasted-nutbread 2021-01-30 15:03:19 -05:00 committed by GitHub
parent 7177694419
commit a802666d8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -58,7 +58,7 @@ class ClipboardReader {
/* /*
Notes: Notes:
document.execCommand('paste') doesn't work on Firefox. document.execCommand('paste') doesn't work on Firefox.
This may be a bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1603985 See: https://bugzilla.mozilla.org/show_bug.cgi?id=1603985
Therefore, navigator.clipboard.readText() is used on Firefox. Therefore, navigator.clipboard.readText() is used on Firefox.
navigator.clipboard.readText() can't be used in Chrome for two reasons: navigator.clipboard.readText() can't be used in Chrome for two reasons:
@ -68,7 +68,12 @@ class ClipboardReader {
non-extension permission for clipboard access. non-extension permission for clipboard access.
*/ */
if (this._isFirefox()) { if (this._isFirefox()) {
try {
return await navigator.clipboard.readText(); return await navigator.clipboard.readText();
} catch (e) {
// Error is undefined, due to permissions
throw new Error('Cannot read clipboard text; check extension permissions');
}
} }
const document = this._document; const document = this._document;
@ -107,7 +112,13 @@ class ClipboardReader {
typeof navigator.clipboard.read === 'function' typeof navigator.clipboard.read === 'function'
) { ) {
// This function is behind the Firefox flag: dom.events.asyncClipboard.dataTransfer // This function is behind the Firefox flag: dom.events.asyncClipboard.dataTransfer
const {files} = await navigator.clipboard.read(); let files;
try {
({files} = await navigator.clipboard.read());
} catch (e) {
return null;
}
for (const file of files) { for (const file of files) {
if (this._mediaUtility.getFileExtensionFromImageMediaType(file.type) !== null) { if (this._mediaUtility.getFileExtensionFromImageMediaType(file.type) !== null) {
return await this._readFileAsDataURL(file); return await this._readFileAsDataURL(file);