document.addEventListener("DOMContentLoaded", function () { const lists = document.querySelectorAll(".copy-icon-list"); // Tạo toast nếu chưa có let toast = document.getElementById("copy-toast"); if (!toast) { toast = document.createElement("div"); toast.id = "copy-toast"; toast.className = "copy-toast"; toast.textContent = "Đã copy!"; document.body.appendChild(toast); } lists.forEach(list => { const icons = list.querySelectorAll(".copy-icon"); icons.forEach(icon => { icon.addEventListener("click", () => { const text = icon.innerText.trim(); // Copy vào clipboard const ta = document.createElement("textarea"); ta.value = text; document.body.appendChild(ta); ta.select(); document.execCommand("copy"); document.body.removeChild(ta); // Hiển thị toast tại icon const rect = icon.getBoundingClientRect(); toast.style.top = `${window.scrollY + rect.top - 35}px`; toast.style.left = `${window.scrollX + rect.left + rect.width / 2}px`; toast.style.transform = "translateX(-50%)"; toast.style.display = "block"; clearTimeout(window._copyToastTimeout); window._copyToastTimeout = setTimeout(() => { toast.style.display = "none"; }, 1500); }); }); }); });