fix: make copy buttons work on HTTP panel pages
Read share links from input fields and fall back to execCommand when Clipboard API is unavailable outside secure contexts. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+46
-5
@@ -10,14 +10,55 @@ function apiUrl(path) {
|
||||
return `${base}${path}`;
|
||||
}
|
||||
|
||||
document.querySelectorAll("[data-copy]").forEach((btn) => {
|
||||
btn.addEventListener("click", async () => {
|
||||
const text = btn.dataset.copy;
|
||||
function copyText(text) {
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
return navigator.clipboard.writeText(text);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const ta = document.createElement("textarea");
|
||||
ta.value = text;
|
||||
ta.setAttribute("readonly", "");
|
||||
ta.style.position = "fixed";
|
||||
ta.style.left = "-9999px";
|
||||
document.body.appendChild(ta);
|
||||
ta.select();
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
if (document.execCommand("copy")) {
|
||||
resolve();
|
||||
} else {
|
||||
reject(new Error("execCommand failed"));
|
||||
}
|
||||
} catch (err) {
|
||||
reject(err);
|
||||
} finally {
|
||||
document.body.removeChild(ta);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
document.querySelectorAll(".copy-row").forEach((row) => {
|
||||
const input = row.querySelector(".copy-input");
|
||||
const btn = row.querySelector(".copy-btn");
|
||||
if (!input || !btn) return;
|
||||
|
||||
input.addEventListener("click", () => {
|
||||
input.select();
|
||||
input.setSelectionRange(0, input.value.length);
|
||||
});
|
||||
|
||||
btn.addEventListener("click", async () => {
|
||||
const text = input.value;
|
||||
if (!text) {
|
||||
toast("没有可复制的内容");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
input.select();
|
||||
input.setSelectionRange(0, text.length);
|
||||
await copyText(text);
|
||||
toast("已复制到剪贴板");
|
||||
} catch {
|
||||
toast("复制失败,请手动选择文本");
|
||||
toast("复制失败,请选中上方链接后 Ctrl+C");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user