Merge pull request #40226 from cjc7373/fix-copy-to-clipboard

fix: eliminate extra lines when copying to clipboard
pull/41437/head
Kubernetes Prow Robot 2023-06-02 22:10:40 -07:00 committed by GitHub
commit a72cd0cbd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 30 deletions

View File

@ -10,36 +10,17 @@
<script type="text/javascript">
function copyCode(elem){
if (document.getElementById(elem)) {
// create hidden text element, if it doesn't already exist
var targetId = "_hiddenCopyText_";
// must use a temporary form element for the selection and copy
target = document.getElementById(targetId);
if (!target) {
var target = document.createElement("textarea");
target.style.position = "absolute";
target.style.left = "-9999px";
target.style.top = "0";
target.id = targetId;
document.body.appendChild(target);
}
target.value = document.getElementById(elem).innerText;
// select the content
target.select();
// copy the selection
var succeed;
try {
succeed = document.execCommand("copy");
} catch(e) {
swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard.");
succeed = false;
}
if (succeed) {
swal("Copied to clipboard: ",elem);
return succeed;
if (navigator.clipboard) {
navigator.clipboard.writeText(document.getElementById(elem).textContent).then(
function () {
swal("Copied to clipboard: ",elem);
},
function () {
swal("Oh, no…","Failed to copy to clipboard: ",elem);
},
);
} else {
swal("Oops!", elem + " not found when trying to copy code");
return false;
swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard.");
}
}
}

View File

@ -26,7 +26,7 @@
</img>
</div>
<div class="includecode" id="{{ $file | anchorize }}">
{{ highlight . $codelang "" }}
{{- highlight . $codelang "" -}}
</div>
</div>
{{ end }}