volodymyrsvirhun commited on
Commit
d0262fc
·
verified ·
1 Parent(s): c9444d2

copy: execCommand fallback for embedded/iframe contexts

Browse files
Files changed (1) hide show
  1. index.html +14 -5
index.html CHANGED
@@ -343,14 +343,23 @@ document.getElementById("closeall").onclick = () => {
343
  document.getElementById("anim").onclick = () => { animating = !animating; };
344
  document.getElementById("copy").onclick = async (e) => {
345
  const btn = e.currentTarget;
 
 
346
  try {
347
- await navigator.clipboard.writeText(
348
- document.getElementById("oneliner").textContent);
349
- btn.textContent = "Copied ✓";
350
- btn.classList.add("ok");
351
  } catch (err) {
352
- btn.textContent = "Copy failed";
 
 
 
 
 
 
 
353
  }
 
 
354
  clearTimeout(btn._t);
355
  btn._t = setTimeout(() => {
356
  btn.textContent = "Copy commands";
 
343
  document.getElementById("anim").onclick = () => { animating = !animating; };
344
  document.getElementById("copy").onclick = async (e) => {
345
  const btn = e.currentTarget;
346
+ const text = document.getElementById("oneliner").textContent;
347
+ let ok = false;
348
  try {
349
+ await navigator.clipboard.writeText(text);
350
+ ok = true;
 
 
351
  } catch (err) {
352
+ // clipboard API blocked (embedded iframe / permission): legacy fallback
353
+ const ta = document.createElement("textarea");
354
+ ta.value = text;
355
+ ta.style.position = "fixed"; ta.style.opacity = "0";
356
+ document.body.appendChild(ta);
357
+ ta.select();
358
+ try { ok = document.execCommand("copy"); } catch (e2) {}
359
+ ta.remove();
360
  }
361
+ btn.textContent = ok ? "Copied ✓" : "Copy failed";
362
+ btn.classList.toggle("ok", ok);
363
  clearTimeout(btn._t);
364
  btn._t = setTimeout(() => {
365
  btn.textContent = "Copy commands";