Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting all anchor tag references to formatted CSV file from Chrome dev console

Let us assume we have a webpage which displays contacts from one person's social network , with a contact name anchored to an href , which points to a unique URL for the user's profile .

I scroll down to the bottom of the page and can see several hundred contacts .

Is it possible for me to export all href occurrences to a csv file with this structure from the developer console via JS ?

Col1 : Name Col2 : profile-url

The end result should be a csv file with name and profileUrl only.

like image 665
Aditya Kaushik Avatar asked May 20 '17 05:05

Aditya Kaushik


1 Answers

This solution is based on the following gist.

Edited the snippet below to your desired scenario:

Exported CSV:

enter image description here

Snippet:

$(document).ready(function() {
  function exportTableToCSV($table, filename) {
    var $headers = $table.find('tr:has(th)'),
      $rows = $table.find('tr:has(td)')
      // Temporary delimiter characters unlikely to be typed by keyboard
      // This is to avoid accidentally splitting the actual contents
      ,
      tmpColDelim = String.fromCharCode(11) // vertical tab character
      ,
      tmpRowDelim = String.fromCharCode(0) // null character
      // actual delimiter characters for CSV format
      ,
      colDelim = '","',
      rowDelim = '"\r\n"';
    // Grab text from table into CSV formatted string
    var csv = '"';
    csv += formatRows($headers.map(grabRow));
    csv += rowDelim;
    csv += formatRows($rows.map(grabRow)) + '"';
    // Data URI
    var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
    $(this)
      .attr({
        'download': filename,
        'href': csvData
        //,'target' : '_blank' //if you want it to open in a new window
      });
    //------------------------------------------------------------
    // Helper Functions 
    //------------------------------------------------------------
    // Format the output so it has the appropriate delimiters
    function formatRows(rows) {
      return rows.get().join(tmpRowDelim)
        .split(tmpRowDelim).join(rowDelim)
        .split(tmpColDelim).join(colDelim);
    }
    // Grab and format a row from the table
    function grabRow(i, row) {

      var $row = $(row);
      //for some reason $cols = $row.find('td') || $row.find('th') won't work...
      var $cols = $row.find('td');
      if (!$cols.length) $cols = $row.find('th');
      return $cols.map(grabCol)
        .get().join(tmpColDelim);
    }
    // Grab and format a column from the table 
    function grabCol(j, col) {
      var $col = $(col),
        $text = $col.text();
      return $text.replace('"', '""'); // escape double quotes
    }
  }
  // This must be a hyperlink
  $("#export").click(function(event) {
    // var outputFile = 'export'
    var outputFile = window.prompt("What do you want to name your output file (Note: This won't have any effect on Safari)") || 'export';
    outputFile = outputFile.replace('.csv', '') + '.csv'

    // CSV
    exportTableToCSV.apply(this, [$('#dvData>table'), outputFile]);

    // IF CSV, don't do event.preventDefault() or return false
    // We actually need this to be a typical hyperlink
  });
});
body {
  font-family: sans-serif;
  font-weight: 300;
  padding-top: 30px;
  color: #666;
}

.container {
  text-align: center;
}

a {
  color: #1C2045;
  font-weight: 350;
}

table {
  font-weight: 300;
  margin: 0px auto;
  border: 1px solid #1C2045;
  padding: 5px;
  color: #666;
}

th,
td {
  border-bottom: 1px solid #dddddd;
  text-align: center;
  margin: 10px;
  padding: 0 10px;
}

.button {
  padding: 5px 20px;
  max-width: 215px;
  line-height: 1.5em;
  text-align: center;
  margin: 5px auto;
  border-radius: 5px;
  border: 1px solid midnightblue;
}

.button a {
  color: midnightblue;
  text-decoration: none;
}

.button a:hover {
  font-weight: 500;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class='container'>
  <div id="dvData">
    <table>
      <tr>
        <th>Name</th>
        <th>Profile URL</th>
      </tr>
      <tr>
        <td>Chris</td>
        <td><a href="http://www.whatever.com">whatever.com</a></td>
      </tr>
      <tr>
        <td>Cornell</td>
        <td><a href="http://www.whatever.com">whatever2.com</a></td>
      </tr>
      <tr>
        <td>Carpet Ride</td>
        <td><a href="http://www.thesky.com">sky.com</a></td>
      </tr>
    </table>
  </div>
  <br/>
  <div class='button'>
    <a href="#" id="export" role="button">Click this text to export Table Data into a CSV File</a>
  </div>
</div>
like image 189
Syden Avatar answered Oct 21 '22 10:10

Syden