Skip to content

Instantly share code, notes, and snippets.

@raincoat
Last active March 2, 2017 21:40
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save raincoat/6062760 to your computer and use it in GitHub Desktop.
Save raincoat/6062760 to your computer and use it in GitHub Desktop.
simulate-trello-clipboard
// copy code from http://stackoverflow.com/questions/17527870/how-does-trello-access-the-users-clipboard/17528590?utm_source=javascriptweekly&utm_medium=email
var utils = {
nodeName: function(node, name){
return !!(node.nodeName.toLowerCase() === name)
}
}
var textareaId = 'simulate-trello-clipboard'
, containerId = textareaId + '-container'
, container
, textarea
var createTextarea = function(){
container = document.querySelector('#' + containerId)
if (!container) {
container = document.createElement('div')
container.id = containerId
container.setAttribute('style', [
, 'position: fixed;'
, 'left: 0px;'
, 'top: 0px;'
, 'width: 0px;'
, 'height: 0px;'
, 'z-index: 100;'
, 'opacity: 0;'
].join(''))
document.body.appendChild(container)
}
container.style.display = 'block'
textarea = document.createElement('textarea')
textarea.setAttribute('style', [
, 'width: 1px;'
, 'height: 1px;'
, 'padding: 0px;'
].join(''))
textarea.id = textareaId
container.innerHTML = ''
container.appendChild(textarea)
textarea.appendChild(document.createTextNode(location.href))
textarea.focus()
textarea.select()
}
var keyDonwMonitor = function(e){
var code = e.keyCode || e.which;
if (!(e.ctrlKey || e.metaKey) || code !== 67) {
return
}
var target = e.target
if (utils.nodeName(target, 'textarea')
|| utils.nodeName(target, 'input')) {
return
}
if (window.getSelection
&& window.getSelection()
&& window.getSelection().toString()) {
return
}
if (document.selection
&& document.selection.createRange().text) {
return
}
setTimeout(createTextarea, 0)
}
var keyUpMonitor = function(e){
var code = e.keyCode || e.which;
if (e.target.id !== textareaId || code !== 67) { return }
container.style.display = 'none'
}
document.addEventListener('keydown', keyDonwMonitor)
document.addEventListener('keyup', keyUpMonitor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment