|
17 | 17 | <script>
|
18 | 18 | document.addEventListener("alpine:init", () => {
|
19 | 19 | Alpine.magic("clipboard", () => {
|
20 |
| - return (subject) => navigator.clipboard.writeText(subject); |
| 20 | + return (subject) => copyToClipboard(subject); |
21 | 21 | });
|
22 | 22 | });
|
| 23 | + |
| 24 | + // copy to clipboard with fallback for protocols |
| 25 | + // and navigator permissions |
| 26 | + function copyToClipboard(strToCopy) { |
| 27 | + if (window.location.protocol === "http:") { |
| 28 | + return fallBackCopy(strToCopy); |
| 29 | + } |
| 30 | + |
| 31 | + if (!(navigator && navigator.clipboard)) { |
| 32 | + return fallBackCopy(strToCopy); |
| 33 | + } |
| 34 | + |
| 35 | + navigator.permissions |
| 36 | + .query({ name: "clipboard-write" }) |
| 37 | + .then((result) => { |
| 38 | + if (result.state == "granted" || result.state == "prompt") { |
| 39 | + navigator.clipboard.writeText(strToCopy).then( |
| 40 | + function () { |
| 41 | + // ignore and digest |
| 42 | + }, |
| 43 | + function () { |
| 44 | + return fallBackCopy(strToCopy); |
| 45 | + } |
| 46 | + ); |
| 47 | + } else { |
| 48 | + return fallBackCopy(strToCopy); |
| 49 | + } |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + function fallBackCopy(strToCopy) { |
| 54 | + const el = document.createElement("textarea"); |
| 55 | + el.value = strToCopy; |
| 56 | + el.setAttribute("readonly", ""); |
| 57 | + el.style.position = "absolute"; |
| 58 | + el.style.left = "-9999px"; |
| 59 | + document.body.appendChild(el); |
| 60 | + el.select(); |
| 61 | + document.execCommand("copy"); |
| 62 | + document.body.removeChild(el); |
| 63 | + } |
23 | 64 | </script>
|
24 | 65 | </head>
|
25 | 66 |
|
26 |
| - <body x-data="{ |
| 67 | + <body |
| 68 | + x-data="{ |
27 | 69 | copied: false,
|
28 | 70 | hits: 0,
|
29 | 71 | async init() {
|
|
34 | 76 | await await `https://hits.goblin.run/hits?url=${window.location.href}`;
|
35 | 77 | this.hits = hitsCount.count
|
36 | 78 | },
|
37 |
| - }"> |
| 79 | + }" |
| 80 | + > |
38 | 81 | <a href="#content" class="sr-only">Skip to main content</a>
|
39 | 82 |
|
40 | 83 | <nav
|
@@ -207,7 +250,5 @@ <h1 class="text-4xl font-bold tracking-tight sm:text-6xl">
|
207 | 250 | </p>
|
208 | 251 | </div>
|
209 | 252 | </footer>
|
210 |
| - |
211 |
| - |
212 | 253 | </body>
|
213 | 254 | </html>
|
0 commit comments