Merge pull request #14 from naturallaw777/copilot/fix-credentials-copy-button
Fix credential copy buttons on non-HTTPS (HTTP) contexts
This commit is contained in:
@@ -310,12 +310,33 @@ async function openCredsModal(unit, name) {
|
|||||||
$credsBody.querySelectorAll(".creds-copy-btn").forEach(function(btn) {
|
$credsBody.querySelectorAll(".creds-copy-btn").forEach(function(btn) {
|
||||||
btn.addEventListener("click", function() {
|
btn.addEventListener("click", function() {
|
||||||
var target = document.getElementById(btn.dataset.target);
|
var target = document.getElementById(btn.dataset.target);
|
||||||
if (target) {
|
if (!target) return;
|
||||||
navigator.clipboard.writeText(target.textContent).then(function() {
|
var text = target.textContent;
|
||||||
|
|
||||||
|
function onSuccess() {
|
||||||
btn.textContent = "Copied!";
|
btn.textContent = "Copied!";
|
||||||
btn.classList.add("copied");
|
btn.classList.add("copied");
|
||||||
setTimeout(function() { btn.textContent = "Copy"; btn.classList.remove("copied"); }, 1500);
|
setTimeout(function() { btn.textContent = "Copy"; btn.classList.remove("copied"); }, 1500);
|
||||||
}).catch(function() {});
|
}
|
||||||
|
|
||||||
|
function fallbackCopy() {
|
||||||
|
var ta = document.createElement("textarea");
|
||||||
|
ta.value = text;
|
||||||
|
ta.style.position = "fixed";
|
||||||
|
ta.style.left = "-9999px";
|
||||||
|
document.body.appendChild(ta);
|
||||||
|
ta.select();
|
||||||
|
try {
|
||||||
|
document.execCommand("copy");
|
||||||
|
onSuccess();
|
||||||
|
} catch (e) {}
|
||||||
|
document.body.removeChild(ta);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (navigator.clipboard && window.isSecureContext) {
|
||||||
|
navigator.clipboard.writeText(text).then(onSuccess).catch(fallbackCopy);
|
||||||
|
} else {
|
||||||
|
fallbackCopy();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user