Skip to content

Instantly share code, notes, and snippets.

@fabiosantoscode
Last active July 22, 2022 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiosantoscode/5464e162680614e151c67a2b6d59f961 to your computer and use it in GitHub Desktop.
Save fabiosantoscode/5464e162680614e151c67a2b6d59f961 to your computer and use it in GitHub Desktop.
Allow pasting tables
// Some focusable element can be rigged to accept user paste events
// Then the HTML is taken from it, which should have a table element if what they copied is a table
focusableElement.onpaste = e => {
const data = e.clipboardData.getData('text/html')
const dom = document.createElement('div')
dom.innerHTML = data
const table = dom.querySelector('table')
const rows = [...table.rows].map(tr => (
[...tr.querySelectorAll('td')].map(td => td.textContent)
))
console.log(rows) // string[][]
outputElement.textContent = JSON.stringify(rows, null, 2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment