Add check for clipboard permissions
This commit is contained in:
parent
439be0740d
commit
fce6da505b
|
|
@ -14,18 +14,47 @@ export default class Clipboard {
|
||||||
|
|
||||||
// ===== PRIVATE METHODS =====
|
// ===== 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) {
|
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) {
|
async _handlePaste(e) {
|
||||||
if (navigator.clipboard.readText) {
|
try {
|
||||||
navigator.clipboard.readText().then(this.onpaste).catch(() => {/* Do nothing */});
|
if (navigator.permissions && navigator.permissions.query) {
|
||||||
} else if (e.clipboardData) {
|
const permission = await navigator.permissions.query({ name: "clipboard-read", allowWithoutGesture: false });
|
||||||
this.onpaste(e.clipboardData.getData('text/plain'));
|
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 =====
|
// ===== PUBLIC METHODS =====
|
||||||
|
|
|
||||||
|
|
@ -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 text = 'Random string for testing';
|
||||||
const clipboard = new Clipboard();
|
const clipboard = new Clipboard();
|
||||||
if (Clipboard.isSupported) {
|
if (Clipboard.isSupported) {
|
||||||
|
|
@ -36,14 +36,14 @@ describe('Automatic Clipboard Sync', function () {
|
||||||
if (!clipboardEvent.clipboardData.items.length) {
|
if (!clipboardEvent.clipboardData.items.length) {
|
||||||
clipboardEvent.clipboardData.items.add(text, "text/plain");
|
clipboardEvent.clipboardData.items.add(text, "text/plain");
|
||||||
}
|
}
|
||||||
clipboard._handleCopy(clipboardEvent);
|
await clipboard._handleCopy(clipboardEvent);
|
||||||
if (navigator.clipboard.writeText) {
|
if (navigator.clipboard.writeText) {
|
||||||
expect(navigator.clipboard.writeText).to.have.been.calledWith(text);
|
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 text = 'Another random string for testing';
|
||||||
const clipboard = new Clipboard();
|
const clipboard = new Clipboard();
|
||||||
clipboard.onpaste = pasterText => expect(pasterText).to.equal(text);
|
clipboard.onpaste = pasterText => expect(pasterText).to.equal(text);
|
||||||
|
|
@ -55,7 +55,7 @@ describe('Automatic Clipboard Sync', function () {
|
||||||
if (!clipboardEvent.clipboardData.items.length) {
|
if (!clipboardEvent.clipboardData.items.length) {
|
||||||
clipboardEvent.clipboardData.items.add(text, "text/plain");
|
clipboardEvent.clipboardData.items.add(text, "text/plain");
|
||||||
}
|
}
|
||||||
clipboard._handlePaste(clipboardEvent);
|
await clipboard._handlePaste(clipboardEvent);
|
||||||
if (navigator.clipboard.readText) {
|
if (navigator.clipboard.readText) {
|
||||||
expect(navigator.clipboard.readText).to.have.been.called;
|
expect(navigator.clipboard.readText).to.have.been.called;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue