Skip to content

Commit

Permalink
Narrow down the (non)HTTP check using the isSecureContent boolean, ho…
Browse files Browse the repository at this point in the history
…wever haven't tested on windows machines.
  • Loading branch information
programbeginnerTW committed Jan 3, 2025
1 parent 25e1380 commit aef9702
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions core/src/main/resources/lib/layout/copyButton/copyButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
);
});
});
Expand Down

0 comments on commit aef9702

Please sign in to comment.