|
10 | 10 | <script type="text/javascript">
|
11 | 11 | function copyCode(elem){
|
12 | 12 | if (document.getElementById(elem)) {
|
13 |
| - // create hidden text element, if it doesn't already exist |
14 |
| - var targetId = "_hiddenCopyText_"; |
15 |
| - // must use a temporary form element for the selection and copy |
16 |
| - target = document.getElementById(targetId); |
17 |
| - if (!target) { |
18 |
| - var target = document.createElement("textarea"); |
19 |
| - target.style.position = "absolute"; |
20 |
| - target.style.left = "-9999px"; |
21 |
| - target.style.top = "0"; |
22 |
| - target.id = targetId; |
23 |
| - document.body.appendChild(target); |
24 |
| - } |
25 |
| - target.value = document.getElementById(elem).innerText; |
26 |
| - // select the content |
27 |
| - target.select(); |
28 |
| - |
29 |
| - // copy the selection |
30 |
| - var succeed; |
31 |
| - try { |
32 |
| - succeed = document.execCommand("copy"); |
33 |
| - } catch(e) { |
34 |
| - swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard."); |
35 |
| - succeed = false; |
36 |
| - } |
37 |
| - if (succeed) { |
38 |
| - swal("Copied to clipboard: ",elem); |
39 |
| - return succeed; |
| 13 | + if (navigator.clipboard) { |
| 14 | + navigator.clipboard.writeText(document.getElementById(elem).textContent).then( |
| 15 | + function () { |
| 16 | + swal("Copied to clipboard: ",elem); |
| 17 | + }, |
| 18 | + function () { |
| 19 | + swal("Oh, no…","Failed to copy to clipboard: ",elem); |
| 20 | + }, |
| 21 | + ); |
40 | 22 | } else {
|
41 |
| - swal("Oops!", elem + " not found when trying to copy code"); |
42 |
| - return false; |
| 23 | + swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard."); |
43 | 24 | }
|
44 | 25 | }
|
45 | 26 | }
|
|
0 commit comments