--
This commit is contained in:
@@ -30,39 +30,50 @@
|
||||
}
|
||||
}
|
||||
|
||||
// ---- JSON-copy button (inside the JSON tab) ----
|
||||
const copyJsonBtn = document.getElementById("tls-copy-json");
|
||||
const rawJsonEl = document.getElementById("tls-raw-json");
|
||||
if (copyJsonBtn && rawJsonEl) {
|
||||
const origLabel = copyJsonBtn.textContent;
|
||||
copyJsonBtn.addEventListener("click", async () => {
|
||||
const text = rawJsonEl.textContent || "";
|
||||
try {
|
||||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
} else {
|
||||
const ta = document.createElement("textarea");
|
||||
ta.value = text;
|
||||
ta.style.position = "fixed";
|
||||
ta.style.opacity = "0";
|
||||
document.body.appendChild(ta);
|
||||
ta.select();
|
||||
try { document.execCommand("copy"); } catch (_) {}
|
||||
document.body.removeChild(ta);
|
||||
}
|
||||
copyJsonBtn.textContent = "コピーしました";
|
||||
copyJsonBtn.classList.add("is-done");
|
||||
setTimeout(() => {
|
||||
copyJsonBtn.textContent = origLabel;
|
||||
copyJsonBtn.classList.remove("is-done");
|
||||
}, 1500);
|
||||
} catch (_) {
|
||||
copyJsonBtn.textContent = "コピー失敗";
|
||||
setTimeout(() => { copyJsonBtn.textContent = origLabel; }, 1500);
|
||||
// ---- Shared copy helper (clipboard API + textarea fallback) ----
|
||||
async function copyText(text) {
|
||||
try {
|
||||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
return true;
|
||||
}
|
||||
const ta = document.createElement("textarea");
|
||||
ta.value = text;
|
||||
ta.style.position = "fixed";
|
||||
ta.style.opacity = "0";
|
||||
document.body.appendChild(ta);
|
||||
ta.select();
|
||||
try { document.execCommand("copy"); } catch (_) {}
|
||||
document.body.removeChild(ta);
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Swap a button's label and is-done class for 1.5s based on action result.
|
||||
function bindCopyButton(btn, getText) {
|
||||
if (!btn) return;
|
||||
const orig = btn.textContent;
|
||||
btn.addEventListener("click", async () => {
|
||||
const ok = await copyText(getText());
|
||||
btn.textContent = ok ? "コピーしました" : "コピー失敗";
|
||||
if (ok) btn.classList.add("is-done");
|
||||
setTimeout(() => {
|
||||
btn.textContent = orig;
|
||||
btn.classList.remove("is-done");
|
||||
}, 1500);
|
||||
});
|
||||
}
|
||||
|
||||
// ---- JSON-copy button (inside the JSON tab) ----
|
||||
const rawJsonEl = document.getElementById("tls-raw-json");
|
||||
bindCopyButton(document.getElementById("tls-copy-json"), () => (rawJsonEl && rawJsonEl.textContent) || "");
|
||||
|
||||
// ---- Copy-link button ----
|
||||
const copyLinkBtn = document.getElementById("tls-copy-link");
|
||||
bindCopyButton(copyLinkBtn, () => (copyLinkBtn && copyLinkBtn.dataset.link) || location.href);
|
||||
|
||||
// ---- PDF / print button ----
|
||||
const printBtn = document.getElementById("tls-print-pdf");
|
||||
if (printBtn) {
|
||||
@@ -88,37 +99,4 @@
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ---- Copy-link button ----
|
||||
const copyBtn = document.getElementById("tls-copy-link");
|
||||
if (copyBtn) {
|
||||
const origLabel = copyBtn.textContent;
|
||||
copyBtn.addEventListener("click", async () => {
|
||||
const link = copyBtn.dataset.link || location.href;
|
||||
try {
|
||||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||
await navigator.clipboard.writeText(link);
|
||||
} else {
|
||||
// Fallback: use a hidden textarea + document.execCommand
|
||||
const ta = document.createElement("textarea");
|
||||
ta.value = link;
|
||||
ta.style.position = "fixed";
|
||||
ta.style.opacity = "0";
|
||||
document.body.appendChild(ta);
|
||||
ta.select();
|
||||
try { document.execCommand("copy"); } catch (_) {}
|
||||
document.body.removeChild(ta);
|
||||
}
|
||||
copyBtn.textContent = "コピーしました";
|
||||
copyBtn.classList.add("is-done");
|
||||
setTimeout(() => {
|
||||
copyBtn.textContent = origLabel;
|
||||
copyBtn.classList.remove("is-done");
|
||||
}, 1500);
|
||||
} catch (_) {
|
||||
copyBtn.textContent = "コピー失敗";
|
||||
setTimeout(() => { copyBtn.textContent = origLabel; }, 1500);
|
||||
}
|
||||
});
|
||||
}
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user