Skip to content

Instantly share code, notes, and snippets.

@sremy
Created February 4, 2017 11:33
Show Gist options
  • Select an option

  • Save sremy/d84f801ec83020fc219b40a43fd64c23 to your computer and use it in GitHub Desktop.

Select an option

Save sremy/d84f801ec83020fc219b40a43fd64c23 to your computer and use it in GitHub Desktop.
CopyToClipboard
// From Github: https://assets-cdn.github.com/assets/github-debf0b32d523b7b17d8bd81d3e9e086bb87af44c5d347e2a0f49bc991d0a77e6.js
function(e, t) {
function n(e) {
return e && e.__esModule ? e : {
"default": e
}
}
function i(e) {
var t = document.createElement("pre");
return t.style.width = "1px", t.style.height = "1px", t.style.position = "fixed", t.style.top = "5px", t.textContent = e, t
}
function r(e) {
var t = getSelection();
if (null != t) {
t.removeAllRanges();
var n = document.createRange();
n.selectNodeContents(e), t.addRange(n), document.execCommand("copy"), t.removeAllRanges()
}
}
function a(e) {
var t = i(e);
document.body.appendChild(t), r(t), document.body.removeChild(t)
}
function s(e) {
e.select(), document.execCommand("copy");
var t = getSelection();
null != t && t.removeAllRanges()
}
function o(e) {
return "INPUT" === e.nodeName || "TEXTAREA" === e.nodeName
}
var u = n(t);
e.on("click", ".js-zeroclipboard", function() {
var e = this,
t = this.getAttribute("data-clipboard-text");
if (t) a(t);
else {
var n = this.closest(".js-zeroclipboard-container"),
i = n.querySelector(".js-zeroclipboard-target");
o(i) ? "hidden" === i.type ? a(i.value) : s(i) : r(i)
}
var l = this.getAttribute("data-copied-hint"),
c = this.getAttribute("aria-label");
l && l !== c && (this.setAttribute("aria-label", l), u["default"](this, "mouseleave").then(function() {
null != c ? e.setAttribute("aria-label", c) : e.removeAttribute("aria-label")
})), this.blur()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment