File size: 795 Bytes
d0eee72
 
 
 
 
6b41207
 
d0eee72
 
 
 
 
6b41207
 
 
 
 
 
 
 
 
 
 
 
d0eee72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
document.addEventListener("click", function (event) {
  var button = event.target.closest("[data-copy]");
  if (!button) return;
  var field = document.getElementById(button.dataset.copy);
  if (!field) return;

  function done() {
    var original = button.textContent;
    button.textContent = "Copied";
    setTimeout(function () {
      button.textContent = original;
    }, 1500);
  }

  field.select();
  field.setSelectionRange(0, field.value.length); // iOS needs the explicit range
  if (navigator.clipboard) {
    navigator.clipboard.writeText(field.value).then(done, function () {
      if (document.execCommand("copy")) done();
    });
  } else if (document.execCommand("copy")) {
    // Older browsers, and any context the async clipboard API is unavailable in.
    done();
  }
});