aboutsummaryrefslogtreecommitdiffhomepage
path: root/script.js
diff options
context:
space:
mode:
authorIgor Tolmachev <me@igorek.dev>2024-04-20 17:40:45 +0900
committerIgor Tolmachev <me@igorek.dev>2024-04-20 17:40:45 +0900
commit93b7de928e8ab59396fcd5ac263aed37afc5ec85 (patch)
treeb04e4c7d8ec596e9a17297d9c63efce6c5e511ec /script.js
parent86d3bd0da5d45f1227ae182f9d87b1a4cc0bfa38 (diff)
downloadpages-93b7de928e8ab59396fcd5ac263aed37afc5ec85.tar.gz
pages-93b7de928e8ab59396fcd5ac263aed37afc5ec85.zip
Add "click to copy" tooltip
Diffstat (limited to 'script.js')
-rw-r--r--script.js18
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 @@
1function 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
9window.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};