Skip to content

Instantly share code, notes, and snippets.

@developit
Last active April 6, 2018 19:10
Show Gist options
  • Select an option

  • Save developit/1f31408ceee234fc0a6dced03da668f2 to your computer and use it in GitHub Desktop.

Select an option

Save developit/1f31408ceee234fc0a6dced03da668f2 to your computer and use it in GitHub Desktop.
function getClipboardText(e) {
return Promise.resolve().then(function(){
return navigator.permissions.query({ name: 'clipboard-read' })
}).then(function(r) {
return r.state=='granted' && navigator.clipboard.readText()
}).catch(Object).then(function(t) {
return typeof t=='string' ? t : e.clipboardData.getData('text/plain')
})
}
/* @example
* document.addEventListener('paste', function (e) {
* getClipboardText(e).then(function (text) {
* console.log(text)
* })
* })
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment