clipboard-copy
Advanced tools
Comparing version 1.3.0 to 1.4.0
21
index.js
module.exports = clipboardCopy | ||
function clipboardCopy (text) { | ||
// A <span> contains the text to copy | ||
var span = document.createElement('span') | ||
// Put the text to copy into a <span> | ||
const span = document.createElement('span') | ||
span.textContent = text | ||
span.style.whiteSpace = 'pre' // Preserve consecutive spaces and newlines | ||
// Preserve consecutive spaces and newlines | ||
span.style.whiteSpace = 'pre' | ||
// An <iframe> isolates the <span> from the page's styles | ||
var iframe = document.createElement('iframe') | ||
const iframe = document.createElement('iframe') | ||
iframe.sandbox = 'allow-same-origin' | ||
// Add the <iframe> to the page | ||
document.body.appendChild(iframe) | ||
var win = iframe.contentWindow | ||
// Add the <span> to the <iframe> | ||
let win = iframe.contentWindow | ||
win.document.body.appendChild(span) | ||
var selection = win.getSelection() | ||
let selection = win.getSelection() | ||
@@ -26,3 +31,3 @@ // Firefox fails to get a selection from <iframe> window, so fallback | ||
var range = win.document.createRange() | ||
const range = win.document.createRange() | ||
selection.removeAllRanges() | ||
@@ -32,3 +37,3 @@ range.selectNode(span) | ||
var success = false | ||
let success = false | ||
try { | ||
@@ -35,0 +40,0 @@ success = win.document.execCommand('copy') |
{ | ||
"name": "clipboard-copy", | ||
"description": "Lightweight copy to clipboard for the web", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"author": { | ||
"name": "Feross Aboukhadijeh", | ||
"email": "feross@feross.org", | ||
"url": "http://feross.org/" | ||
"url": "https://feross.org" | ||
}, | ||
@@ -18,10 +18,10 @@ "bugs": { | ||
"keywords": [ | ||
"browser", | ||
"clipboard", | ||
"clipboard copy", | ||
"clipboard.js", | ||
"copy", | ||
"clipboard", | ||
"simple", | ||
"browser", | ||
"copy to clipboard", | ||
"clipboard.js", | ||
"clipboard copy", | ||
"modern browsers" | ||
"modern browsers", | ||
"simple" | ||
], | ||
@@ -28,0 +28,0 @@ "license": "MIT", |
@@ -22,2 +22,4 @@ # clipboard-copy [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] | ||
**Supported browsers:** Chrome, Firefox, Edge, Safari, IE11. | ||
Works in the browser with [browserify](http://browserify.org/)! | ||
@@ -56,4 +58,8 @@ | ||
## testing | ||
Testing this module is currently a manual process. Open `test.html` in your web browser and follow the short instructions. The web page will always load the latest version of the module, no bundling is necessary. | ||
## license | ||
MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org). |
Sorry, the diff of this file is not supported yet
6614
8
47
64