diff options
| author | Igor Tolmachev <me@igorek.dev> | 2024-04-20 17:40:45 +0900 |
|---|---|---|
| committer | Igor Tolmachev <me@igorek.dev> | 2024-04-20 17:40:45 +0900 |
| commit | 93b7de928e8ab59396fcd5ac263aed37afc5ec85 (patch) | |
| tree | b04e4c7d8ec596e9a17297d9c63efce6c5e511ec /script.js | |
| parent | 86d3bd0da5d45f1227ae182f9d87b1a4cc0bfa38 (diff) | |
| download | pages-93b7de928e8ab59396fcd5ac263aed37afc5ec85.tar.gz pages-93b7de928e8ab59396fcd5ac263aed37afc5ec85.zip | |
Add "click to copy" tooltip
Diffstat (limited to 'script.js')
| -rw-r--r-- | script.js | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/script.js b/script.js new file mode 100644 index 0000000..b16142f --- /dev/null +++ b/script.js | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | function copy(text, tooltip) { | ||
| 2 | return () => { | ||
| 3 | navigator.clipboard.writeText(text); | ||
| 4 | tooltip.textContent = "[copied]"; | ||
| 5 | setTimeout(() => (tooltip.textContent = "[click to copy]"), 1000); | ||
| 6 | }; | ||
| 7 | } | ||
| 8 | |||
| 9 | window.onload = () => { | ||
| 10 | document.querySelectorAll(".copy").forEach((element) => { | ||
| 11 | const tooltip = document.createElement("span"); | ||
| 12 | tooltip.classList.add("copy-tooltip"); | ||
| 13 | tooltip.textContent = "[click to copy]"; | ||
| 14 | |||
| 15 | element.onclick = copy(element.attributes["copy-text"]?.value, tooltip); | ||
| 16 | element.appendChild(tooltip); | ||
| 17 | }); | ||
| 18 | }; | ||
