Skip to content

Commit f0be0ca

Browse files
authored
[JENKINS-21052] Warn user that the copy button requires HTTPS (#7665)
Warn user that the copy button requires HTTPS Users that run Jenkins over an unencrypted (HTTP) connection will see the copy button, but it does not perform a copy. Browsers will only allow access to the clipboard if the page is in a secure context. With this change, the text that is displayed when the user clicks the copy button from an insecure context will alert the user that copy requires a secure connection. https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript describes the alternatives in more detail. https://issues.jenkins.io/browse/JENKINS-21052 reported that the copy button does not work. I've confirmed that it works when using an HTTPS connection.
1 parent bc7f06c commit f0be0ca

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

core/src/main/resources/lib/layout/copyButton/copyButton.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,35 @@ Behaviour.specify(
44
0,
55
function (copyButton) {
66
copyButton.addEventListener("click", () => {
7-
// Make an invisible textarea element containing the text
8-
const fakeInput = document.createElement("textarea");
9-
fakeInput.value = copyButton.getAttribute("text");
10-
fakeInput.style.width = "1px";
11-
fakeInput.style.height = "1px";
12-
fakeInput.style.border = "none";
13-
fakeInput.style.padding = "0px";
14-
fakeInput.style.position = "absolute";
15-
fakeInput.style.top = "-99999px";
16-
fakeInput.style.left = "-99999px";
17-
fakeInput.setAttribute("tabindex", "-1");
18-
document.body.appendChild(fakeInput);
7+
if (isSecureContext) {
8+
// Make an invisible textarea element containing the text
9+
const fakeInput = document.createElement("textarea");
10+
fakeInput.value = copyButton.getAttribute("text");
11+
fakeInput.style.width = "1px";
12+
fakeInput.style.height = "1px";
13+
fakeInput.style.border = "none";
14+
fakeInput.style.padding = "0px";
15+
fakeInput.style.position = "absolute";
16+
fakeInput.style.top = "-99999px";
17+
fakeInput.style.left = "-99999px";
18+
fakeInput.setAttribute("tabindex", "-1");
19+
document.body.appendChild(fakeInput);
1920

20-
// Select the text and copy it to the clipboard
21-
fakeInput.select();
22-
navigator.clipboard.writeText(fakeInput.value);
21+
// Select the text and copy it to the clipboard
22+
fakeInput.select();
23+
navigator.clipboard.writeText(fakeInput.value);
2324

24-
// Remove the textarea element
25-
document.body.removeChild(fakeInput);
25+
// Remove the textarea element
26+
document.body.removeChild(fakeInput);
2627

27-
// Show the completion message
28-
hoverNotification(copyButton.getAttribute("message"), copyButton);
28+
// Show the completion message
29+
hoverNotification(copyButton.getAttribute("message"), copyButton);
30+
} else {
31+
hoverNotification(
32+
"Copy is only supported with a secure (HTTPS) connection",
33+
copyButton
34+
);
35+
}
2936
});
3037
}
3138
);

0 commit comments

Comments
 (0)