简单的方法来添加'复制到剪贴板'GitHub降价? [英] Easy way to add 'copy to clipboard' to GitHub markdown?

查看:123
本文介绍了简单的方法来添加'复制到剪贴板'GitHub降价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体来说,我有安装代码块,我希望用户能够快速复制并粘贴到终端中。我想要一个按钮来为代码块复制到剪贴板。由于git克隆网址有一个复制到剪贴板按钮,我想知道是否可以捎带,或者不知道是否有一些相对简单的我可以添加到MD来实现这一点。或者,这是不可能的处理和'保险'的MD文本通过?

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 code block. Since there's a 'copy to clipboard' button for the git clone URLs, I was wondering if I could piggy back off that or if not whether there was some relatively simple I could add to the MD to make this happen. Or is this simply not possible with the processing and 'safication' the MD text goes through?

推荐答案

我认为这不是你想要什么,但如果你想复制,你可以通过运行书签和添加一个复制按钮来完成它。

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

这篇关于简单的方法来添加'复制到剪贴板'GitHub降价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆