Skip to content

Commit

Permalink
Actually adjust copy button logic to new API (#8554)
Browse files Browse the repository at this point in the history
* Actually adjust copy button logic to new API

Remove the obsolete textarea creation and correctly handle other permission errors

* Remove obsolete check if running as test

HTMLUnit now supports `isSecureContext`

---------

Co-authored-by: Mark Waite <[email protected]>
  • Loading branch information
D3SOX and MarkEWaite authored Oct 6, 2023
1 parent e740d1b commit b29b457
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions core/src/main/resources/lib/layout/copyButton/copyButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,20 @@ Behaviour.specify(
0,
function (copyButton) {
copyButton.addEventListener("click", () => {
// HTMLUnit 2.70.0 does not recognize isSecureContext
// https://issues.jenkins.io/browse/JENKINS-70895
if (!window.isRunAsTest && isSecureContext) {
// Make an invisible textarea element containing the text
const fakeInput = document.createElement("textarea");
fakeInput.value = copyButton.getAttribute("text");
fakeInput.style.width = "1px";
fakeInput.style.height = "1px";
fakeInput.style.border = "none";
fakeInput.style.padding = "0px";
fakeInput.style.position = "absolute";
fakeInput.style.top = "-99999px";
fakeInput.style.left = "-99999px";
fakeInput.setAttribute("tabindex", "-1");
document.body.appendChild(fakeInput);

// Select the text and copy it to the clipboard
fakeInput.select();
navigator.clipboard.writeText(fakeInput.value);

// Remove the textarea element
document.body.removeChild(fakeInput);

// Show the completion message
hoverNotification(copyButton.getAttribute("message"), copyButton);
if (isSecureContext) {
// Copy the text to the clipboard
navigator.clipboard
.writeText(copyButton.getAttribute("text"))
.then(() => {
// Show the completion message
hoverNotification(copyButton.getAttribute("message"), copyButton);
})
.catch(() => {
hoverNotification(
"Could not get permission to write to clipboard",
copyButton,
);
});
} else {
hoverNotification(
"Copy is only supported with a secure (HTTPS) connection",
Expand Down

0 comments on commit b29b457

Please sign in to comment.