Last active
July 22, 2022 21:43
-
-
Save fabiosantoscode/5464e162680614e151c67a2b6d59f961 to your computer and use it in GitHub Desktop.
Allow pasting tables
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
// 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