From 93b7de928e8ab59396fcd5ac263aed37afc5ec85 Mon Sep 17 00:00:00 2001 From: Igor Tolmachev Date: Sat, 20 Apr 2024 17:40:45 +0900 Subject: Add "click to copy" tooltip --- script.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 script.js (limited to 'script.js') diff --git a/script.js b/script.js new file mode 100644 index 0000000..b16142f --- /dev/null +++ b/script.js @@ -0,0 +1,18 @@ +function copy(text, tooltip) { + return () => { + navigator.clipboard.writeText(text); + tooltip.textContent = "[copied]"; + setTimeout(() => (tooltip.textContent = "[click to copy]"), 1000); + }; +} + +window.onload = () => { + document.querySelectorAll(".copy").forEach((element) => { + const tooltip = document.createElement("span"); + tooltip.classList.add("copy-tooltip"); + tooltip.textContent = "[click to copy]"; + + element.onclick = copy(element.attributes["copy-text"]?.value, tooltip); + element.appendChild(tooltip); + }); +}; -- cgit v1.2.3