Travis build: 694 [ci skip]
This commit is contained in:
43
README.md
43
README.md
@ -84,6 +84,7 @@
|
|||||||
|
|
||||||
* [`arrayToHtmlList`](#arraytohtmllist)
|
* [`arrayToHtmlList`](#arraytohtmllist)
|
||||||
* [`bottomVisible`](#bottomvisible)
|
* [`bottomVisible`](#bottomvisible)
|
||||||
|
* [`copyToClipboard`](#copytoclipboard)
|
||||||
* [`currentURL`](#currenturl)
|
* [`currentURL`](#currenturl)
|
||||||
* [`detectDeviceType`](#detectdevicetype)
|
* [`detectDeviceType`](#detectdevicetype)
|
||||||
* [`elementIsVisibleInViewport`](#elementisvisibleinviewport)
|
* [`elementIsVisibleInViewport`](#elementisvisibleinviewport)
|
||||||
@ -1483,6 +1484,48 @@ bottomVisible(); // true
|
|||||||
<br>[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
|
### copyToClipboard
|
||||||
|
|
||||||
|
Copy a string to the clipboard. Only works as a result of user action (i.e. inside a `click` event listener).
|
||||||
|
|
||||||
|
Create a new `<textarea>` element, fill it with the supplied data and add it to the HTML document.
|
||||||
|
Use `Selection.getRangeAt()`to store the selected range (if any).
|
||||||
|
Use `document.execCommand('copy')` to copy to the clipboard.
|
||||||
|
Remove the `<textarea>` element from the HTML document.
|
||||||
|
Finally, use `Selection().addRange()` to recover the original selected range (if any).
|
||||||
|
|
||||||
|
```js
|
||||||
|
const copyToClipboard = str => {
|
||||||
|
const el = document.createElement('textarea');
|
||||||
|
el.value = str;
|
||||||
|
el.setAttribute('readonly', '');
|
||||||
|
el.style.position = 'absolute';
|
||||||
|
el.style.left = '-9999px';
|
||||||
|
document.body.appendChild(el);
|
||||||
|
const selected =
|
||||||
|
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
|
||||||
|
el.select();
|
||||||
|
document.execCommand('copy');
|
||||||
|
document.body.removeChild(el);
|
||||||
|
if (selected) {
|
||||||
|
document.getSelection().removeAllRanges();
|
||||||
|
document.getSelection().addRange(selected);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Examples</summary>
|
||||||
|
|
||||||
|
```js
|
||||||
|
copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|
||||||
### currentURL
|
### currentURL
|
||||||
|
|
||||||
Returns the current URL.
|
Returns the current URL.
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@ -16,7 +16,8 @@ const copyToClipboard = str => {
|
|||||||
el.style.position = 'absolute';
|
el.style.position = 'absolute';
|
||||||
el.style.left = '-9999px';
|
el.style.left = '-9999px';
|
||||||
document.body.appendChild(el);
|
document.body.appendChild(el);
|
||||||
const selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
|
const selected =
|
||||||
|
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
|
||||||
el.select();
|
el.select();
|
||||||
document.execCommand('copy');
|
document.execCommand('copy');
|
||||||
document.body.removeChild(el);
|
document.body.removeChild(el);
|
||||||
@ -24,7 +25,7 @@ const copyToClipboard = str => {
|
|||||||
document.getSelection().removeAllRanges();
|
document.getSelection().removeAllRanges();
|
||||||
document.getSelection().addRange(selected);
|
document.getSelection().addRange(selected);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
Reference in New Issue
Block a user