clipboard-copy
Advanced tools
Comparing version 3.2.0 to 4.0.0
51
index.js
@@ -6,15 +6,18 @@ /*! clipboard-copy. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */ | ||
function clipboardCopy (text) { | ||
function makeError () { | ||
return new DOMException('The request is not allowed', 'NotAllowedError') | ||
} | ||
async function copyClipboardApi (text) { | ||
// Use the Async Clipboard API when available. Requires a secure browsing | ||
// context (i.e. HTTPS) | ||
if (navigator.clipboard) { | ||
return navigator.clipboard.writeText(text).catch(function (err) { | ||
throw (err !== undefined ? err : new DOMException('The request is not allowed', 'NotAllowedError')) | ||
}) | ||
if (!navigator.clipboard) { | ||
throw makeError() | ||
} | ||
await navigator.clipboard.writeText(text) | ||
} | ||
// ...Otherwise, use document.execCommand() fallback | ||
async function copyExecCommand (text) { | ||
// Put the text to copy into a <span> | ||
var span = document.createElement('span') | ||
const span = document.createElement('span') | ||
span.textContent = text | ||
@@ -31,4 +34,4 @@ | ||
// Make a selection object representing the range of text selected by the user | ||
var selection = window.getSelection() | ||
var range = window.document.createRange() | ||
const selection = window.getSelection() | ||
const range = window.document.createRange() | ||
selection.removeAllRanges() | ||
@@ -39,16 +42,26 @@ range.selectNode(span) | ||
// Copy text to the clipboard | ||
var success = false | ||
let success = false | ||
try { | ||
success = window.document.execCommand('copy') | ||
if (!success) throw makeError() | ||
} finally { | ||
// Cleanup | ||
selection.removeAllRanges() | ||
window.document.body.removeChild(span) | ||
} | ||
} | ||
async function clipboardCopy (text) { | ||
try { | ||
await copyClipboardApi(text) | ||
} catch (err) { | ||
console.log('error', err) | ||
// ...Otherwise, use document.execCommand() fallback | ||
try { | ||
await copyExecCommand(text) | ||
} catch (err2) { | ||
throw (err2 || err || makeError()) | ||
} | ||
} | ||
// Cleanup | ||
selection.removeAllRanges() | ||
window.document.body.removeChild(span) | ||
return success | ||
? Promise.resolve() | ||
: Promise.reject(new DOMException('The request is not allowed', 'NotAllowedError')) | ||
return Promise.resolve() | ||
} |
{ | ||
"name": "clipboard-copy", | ||
"description": "Lightweight copy to clipboard for the web", | ||
"version": "3.2.0", | ||
"version": "4.0.0", | ||
"author": { | ||
@@ -14,2 +14,3 @@ "name": "Feross Aboukhadijeh", | ||
"devDependencies": { | ||
"ecstatic": "^4.1.4", | ||
"standard": "*" | ||
@@ -35,3 +36,4 @@ }, | ||
"scripts": { | ||
"test": "standard" | ||
"test": "standard", | ||
"test-browser-local": "ecstatic -p 4000 ." | ||
}, | ||
@@ -38,0 +40,0 @@ "funding": [ |
@@ -24,3 +24,3 @@ # clipboard-copy [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![size][size-image]][size-url] [![javascript style guide][standard-image]][standard-url] | ||
**Supported browsers:** Chrome, Firefox, Edge, Safari, IE11. | ||
**Supported browsers:** Chrome, Firefox, Edge, Safari. | ||
@@ -49,3 +49,3 @@ Works in the browser with [browserify](http://browserify.org/)! | ||
Copy the given text to the user's clipboard. Returns `success`, a promise that resolves if the copy was successful and rejects if the copy failed. | ||
Copy the given text to the user's clipboard. Returns `successPromise`, a promise that resolves if the copy was successful and rejects if the copy failed. | ||
@@ -52,0 +52,0 @@ Note: in most browsers, copying to the clipboard is only allowed if `copy()` is |
6631
56
2
339804