Skip to content

Instantly share code, notes, and snippets.

@leostaples
Created February 12, 2016 12:07
Show Gist options
  • Save leostaples/fc240d1f899a2f5730c7 to your computer and use it in GitHub Desktop.
Save leostaples/fc240d1f899a2f5730c7 to your computer and use it in GitHub Desktop.
Export to CSV bookmarklet
javascript:(function()%7Bfunction getJavaScript(a%2Cb)%7Bvar c%3Ddocument.createElement('script')%3Bc.src%3Da%3Bvar d%3Ddocument.getElementsByTagName('head')%5B0%5D%2Cdone%3Dfalse%3Bc.onload%3Dc.onreadystatechange%3Dfunction()%7Bif(!done%26%26(!this.readyState%7C%7Cthis.readyState%3D%3D'loaded'%7C%7Cthis.readyState%3D%3D'complete'))%7Bdone%3Dtrue%3Bb()%3Bc.onload%3Dc.onreadystatechange%3Dnull%3Bd.removeChild(c)%7D%7D%3Bd.appendChild(c)%7Dfunction addCSVLinks()%7BjQuery('.csvLink').remove()%3BjQuery('table').each(function(a)%7BjQuery(this).attr('data-csvtable'%2Ca).before('<a href%3D"%23" class%3D"csvLink" data-forcsvtable%3D"'%2Ba%2B'">Export to CSV<%2Fa>')%7D)%3BjQuery('.csvLink').click(function()%7Bvar b%3D''%3Bvar c%3DjQuery(this).attr('data-forcsvtable')%3BjQuery('table%5Bdata-csvtable%3D"'%2Bc%2B'"%5D tr').each(function()%7Bif(jQuery('th'%2Cthis).length%3D%3D0)%7BjQuery('td'%2Cthis).each(function(a)%7Bif(a!%3D0)%7Bb%2B%3D'%2C'%7Db%2B%3D'"'%2BjQuery(this).text().replace(%2F%5Cxa0%2Fg%2C' ').replace(%2F%5Cr%3F%5Cn%7C%5Cr%2Fg%2C'').replace(%2F"%2Fg%2C'""')%2B'"'%7D)%7Delse%7BjQuery('th'%2Cthis).each(function(a)%7Bif(a!%3D0)%7Bb%2B%3D'%2C'%7Db%2B%3D'"'%2BjQuery(this).text().replace(%2F%5Cxa0%2Fg%2C' ').replace(%2F%5Cr%3F%5Cn%7C%5Cr%2Fg%2C'').replace(%2F"%2Fg%2C'""')%2B'"'%7D)%7Db%2B%3D'%5Cr%5Cn'%7D)%3BjQuery('.csvLink').remove()%3BdownloadCSVFile('TableExport.csv'%2C'text%2Fcsv'%2Cb)%7D)%7Dfunction downloadCSVFile(a%2Cb%2Cc)%7Bvar d%3Ddocument.createElement('a')%3Bd.setAttribute('href'%2C'data%3A'%2Bb%2B'%3Bcharset%3Dutf-8%2C'%2BencodeURIComponent(c))%3Bd.setAttribute('download'%2Ca)%3Bdocument.body.appendChild(d)%3Bd.click()%3Bdocument.body.removeChild(d)%7Dif(typeof jQuery%3D%3D'undefined')%7BgetJavaScript('%2F%2Fcode.jquery.com%2Fjquery-latest.min.js'%2Cfunction()%7BaddCSVLinks()%7D)%7Delse%7BaddCSVLinks()%7D%7D)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment