将 'copy to clipboard' 添加到 GitHub 降价的简单方法?

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

具体来说,我有一些安装代码块,我希望用户能够快速复制并粘贴到终端中。我想要代码块的 'copy to clipboard' 按钮。由于 git 克隆 URL 有一个 'copy to clipboard' 按钮,我想知道我是否可以利用它,或者如果不能,我是否可以将一些相对简单的东西添加到 MD 中来实现这一点。或者这根本不可能通过处理和 'safication' MD 文本通过?

我觉得这不是你想要的,但是如果你想复制,你可以通过运行书签并添加一个复制按钮来完成。

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])
  })
})

复制按钮现已成为现实(2021 年 5 月),因为 tweeted by Nat Friedman

We just added a "Copy" button to all code blocks on GitHub.