Last active
March 2, 2017 21:40
-
-
Save raincoat/6062760 to your computer and use it in GitHub Desktop.
simulate-trello-clipboard
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?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