Easy way to add 'copy to clipboard' to GitHub markdown?

后端 未结 1 1155
后悔当初
后悔当初 2021-02-03 22:44

Specifically, I have blocks of code for install that I want the user to be able to quickly copy and paste into a terminal. I\'d like a button to \'copy to clipboard\' for the co

1条回答
  •  花落未央
    2021-02-03 23:13

    I think that it is not what you want, but if you want to copy, you can do it by running the bookmarklet and adding a copy button.

    var copy = function(target) {
        var textArea = document.createElement('textarea')
        textArea.setAttribute('style','width:1px;border:0;opacity:0;')
        document.body.appendChild(textArea)
        textArea.value = target.innerHTML
        textArea.select()
        document.execCommand('copy')
        document.body.removeChild(textArea)
    }
    
    var pres = document.querySelectorAll(".comment-body > pre")
    pres.forEach(function(pre){
      var button = document.createElement("button")
      button.className = "btn btn-sm"
      button.innerHTML = "copy"
      pre.parentNode.insertBefore(button, pre)
      button.addEventListener('click', function(e){
        e.preventDefault()
        copy(pre.childNodes[0])
      })
    })
    

    0 讨论(0)
提交回复
热议问题