From fce6da505bc57ab6a842f737753633673808d4db Mon Sep 17 00:00:00 2001 From: Juanjo Diaz Date: Sun, 16 Oct 2022 12:10:44 +0200 Subject: [PATCH] Add check for clipboard permissions --- core/clipboard.js | 43 ++++++++++++++++++++++++++++++++++------- tests/test.clipboard.js | 8 ++++---- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/core/clipboard.js b/core/clipboard.js index a4a68132..29b2a356 100644 --- a/core/clipboard.js +++ b/core/clipboard.js @@ -14,18 +14,47 @@ export default class Clipboard { // ===== PRIVATE METHODS ===== - _handleCopy(e) { + async _handleCopy(e) { + try { + if (navigator.permissions && navigator.permissions.query) { + const permission = await navigator.permissions.query({ name: "clipboard-write", allowWithoutGesture: false }); + if (permission.state === 'denied') return; + } + } catch (err) { + // Some browsers might error due to lack of support, e.g. Firefox. + } + if (navigator.clipboard.writeText) { - navigator.clipboard.writeText(e.clipboardData.getData('text/plain')).catch(() => {/* Do nothing */}); + try { + await navigator.clipboard.writeText(e.clipboardData.getData('text/plain')); + } catch (e) { + /* Do nothing */ + } } } - _handlePaste(e) { - if (navigator.clipboard.readText) { - navigator.clipboard.readText().then(this.onpaste).catch(() => {/* Do nothing */}); - } else if (e.clipboardData) { - this.onpaste(e.clipboardData.getData('text/plain')); + async _handlePaste(e) { + try { + if (navigator.permissions && navigator.permissions.query) { + const permission = await navigator.permissions.query({ name: "clipboard-read", allowWithoutGesture: false }); + if (permission.state === 'denied') return; + } + } catch (err) { + // Some browsers might error due to lack of support, e.g. Firefox. } + + let data; + if (navigator.clipboard.readText) { + try { + data = await navigator.clipboard.readText(); + } catch (e) { + /* Do nothing */ + return; + } + } else if (e.clipboardData) { + data = e.clipboardData.getData('text/plain'); + } + this.onpaste(data); } // ===== PUBLIC METHODS ===== diff --git a/tests/test.clipboard.js b/tests/test.clipboard.js index 2ef00574..7b400e60 100644 --- a/tests/test.clipboard.js +++ b/tests/test.clipboard.js @@ -25,7 +25,7 @@ describe('Automatic Clipboard Sync', function () { }); } - it('incoming clipboard data from the server is copied to the local clipboard', function () { + it('incoming clipboard data from the server is copied to the local clipboard', async function () { const text = 'Random string for testing'; const clipboard = new Clipboard(); if (Clipboard.isSupported) { @@ -36,14 +36,14 @@ describe('Automatic Clipboard Sync', function () { if (!clipboardEvent.clipboardData.items.length) { clipboardEvent.clipboardData.items.add(text, "text/plain"); } - clipboard._handleCopy(clipboardEvent); + await clipboard._handleCopy(clipboardEvent); if (navigator.clipboard.writeText) { expect(navigator.clipboard.writeText).to.have.been.calledWith(text); } } }); - it('should copy local pasted data to the server clipboard', function () { + it('should copy local pasted data to the server clipboard', async function () { const text = 'Another random string for testing'; const clipboard = new Clipboard(); clipboard.onpaste = pasterText => expect(pasterText).to.equal(text); @@ -55,7 +55,7 @@ describe('Automatic Clipboard Sync', function () { if (!clipboardEvent.clipboardData.items.length) { clipboardEvent.clipboardData.items.add(text, "text/plain"); } - clipboard._handlePaste(clipboardEvent); + await clipboard._handlePaste(clipboardEvent); if (navigator.clipboard.readText) { expect(navigator.clipboard.readText).to.have.been.called; }