Last active
May 16, 2018 13:59
-
-
Save anthonybrown/e97dbd39127a2a464bc04df8bb133610 to your computer and use it in GitHub Desktop.
log the clipboard contents
This file contains 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
document.body.innerHTML = 'Paste or drop items onto this page. View results in console.'; | |
function getPayload(item) { | |
const kind = item.kind; | |
switch (kind) { | |
case 'string': return new Promise(res => item.getAsString(res)); | |
case 'file': return Promise.resolve(item.getAsFile()); | |
default: throw new Error('unknown item kind! ' + kind); | |
} | |
} | |
function logObj(type, obj){ | |
console.log(`%c ${type} event`, 'font-weight: bold', new Date().toLocaleTimeString()); | |
const getPayloadAndType = item => { | |
const type = item.type; | |
return getPayload(item).then(payload => ({type, payload})); | |
}; | |
Array.from(obj.items) | |
.sort((a,b) => a.type.localeCompare(b.type)) | |
.forEach(item => Promise.resolve().then(_ => getPayloadAndType(item)).then(console.log)); | |
} | |
document.onpaste = e => { logObj(e.type, e.clipboardData)}; | |
document.ondrop = e => { e.preventDefault(); logObj(e.type, e.dataTransfer)}; | |
document.ondragover = e => { e.preventDefault(); }; | |
This looks like a JavaScript file. Click this bar to format it.No 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From Paul Irish