diff --git a/core/src/main/resources/lib/layout/copyButton/copyButton.js b/core/src/main/resources/lib/layout/copyButton/copyButton.js index 5a04514a5bcd..7bf0f853ba92 100644 --- a/core/src/main/resources/lib/layout/copyButton/copyButton.js +++ b/core/src/main/resources/lib/layout/copyButton/copyButton.js @@ -3,25 +3,33 @@ Behaviour.specify( "copyButton", 0, function (copyButton) { - // Check if the connection is secure by evaluating the protocol - const isHttps = window.location.protocol === "https:"; + // Check if the context is secure + if (!isSecureContext) { + console.log("Disabling the button because the connection is insecure."); - if (!isHttps) { + // Disable the button and add a hover notification copyButton.disabled = true; - copyButton.title = "Copy doesn't work in insecure (HTTP) contexts"; + copyButton.title = "Copy is only supported with a secure (HTTPS) connection"; + + // Add a hover notification when the button is disabled + copyButton.addEventListener("mouseenter", () => { + hoverNotification( + "Copy doesn't work in insecure (HTTP) contexts. Please use HTTPS.", + copyButton + ); + }); + return; } + console.log("Enabling the button because the connection is secure."); - // Enable the button + // If secure, enable the button and set up the click handler copyButton.disabled = false; - copyButton.title = "Click to copy the text"; - - // Add click event listener copyButton.addEventListener("click", () => { let text = copyButton.getAttribute("text"); - // If the "ref" attribute is present, fetch the target element's text + // If the button has a "ref" attribute, fetch the target element's text if (copyButton.hasAttribute("ref")) { const ref = copyButton.getAttribute("ref"); const target = document.getElementById(ref); @@ -34,13 +42,14 @@ Behaviour.specify( navigator.clipboard .writeText(text) .then(() => { + // Show a success message hoverNotification(copyButton.getAttribute("message") || "Copied!", copyButton); }) - .catch((err) => { - console.error("Failed to copy text:", err); + .catch(() => { + // Show an error message if the copy fails hoverNotification( "Could not get permission to write to clipboard", - copyButton, + copyButton ); }); });