// Webhooker client-side JavaScript console.log("Webhooker loaded"); // Copy-to-clipboard, as progressive enhancement. // // Markup renders each copy button with the `hidden` attribute and a // `data-copy-target` pointing at the id of the element holding the // text. This script reveals a button only once it has both a resolvable // target and a usable Clipboard API, so a browser without either shows // no button at all and the text stays selectable. (function () { "use strict"; var revertDelayMs = 2000; function flash(button, message) { var original = button.getAttribute("data-copy-label"); button.textContent = message; window.setTimeout(function () { button.textContent = original; }, revertDelayMs); } function wire(button) { var target = document.getElementById( button.getAttribute("data-copy-target") ); if (!target) { return; } button.setAttribute("data-copy-label", button.textContent); button.addEventListener("click", function () { navigator.clipboard.writeText(target.textContent.trim()).then( function () { flash(button, "Copied"); }, function () { flash(button, "Copy failed"); } ); }); button.removeAttribute("hidden"); } function init() { if (!navigator.clipboard || !navigator.clipboard.writeText) { return; } var buttons = document.querySelectorAll("[data-copy-target]"); for (var i = 0; i < buttons.length; i++) { wire(buttons[i]); } } if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", init); } else { init(); } })();