Forked from raincoat/simulate-trello-clipboard.js
Last active
August 29, 2015 14:03
-
-
Save TexRx/4e159c2a83fb1a6d35c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// copy code from http://stackoverflow.com/questions/17527870/how-does-trello-access-the-users-clipboard/17528590 | |
(function() { | |
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 keyDownMonitor = 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', keyDownMonitor); | |
document.addEventListener('keyup', keyUpMonitor); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment