/* console.save() from dev tools code snippets 
 * http://bgrins.github.io/devtools-snippets/#console-save
 */

(function(console){

  console.save = function(data, filename){

    if(!data) {
      console.error('Console.save: No data')
      return;
    }

    if(!filename) filename = 'console.json'

    if(typeof data === "object"){
      data = JSON.stringify(data, undefined, 4)
    }

    var blob = new Blob([data], {type: 'text/json'}),
    e    = document.createEvent('MouseEvents'),
    a    = document.createElement('a')

    a.download = filename
    a.href = window.URL.createObjectURL(blob)
    a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':')
    e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
    a.dispatchEvent(e)
  }
})(console)


(function() {
  var tables = $('table'),
      entries = [],
      nodes = $('#content').children();

  function process_date(node) {
    var date = node.text().split(' '); 

    return {
      'month': date[0], 
      'day': date[1].replace(/\,/g,''), 
      'year': date[2]
    }
  }

  function process_table(node, date) {
    var rows = node.children('tbody').children(),
        new_entries = [];

    for (var j=0; j < rows.length; j++) {
      row = $(rows[j])[0];

      if (row.classList[0] !== 'title') {
        var entry = {};
        
        entry.exercise = $(row.children[0]).text();
        entry.sets = $(row.children[2]).text();
        entry.reps = $(row.children[3]).text();
        entry.weight = $(row.children[4]).text().trim();
        entry.month = date.month;
        entry.day = date.day;
        entry.year = date.year;

        new_entries.push(entry);
      }
    }

    return new_entries
  }

  for (var i=0; i < nodes.length; i++) {
    var node = $(nodes[i]),
      date;
    
    if (node.prop('tagName') === "H2") {
      date = process_date(node); 
    } else if (node.prop('tagName') === "TABLE") {
      var new_entries = process_table(node, date);
      entries = entries.concat(new_entries);
    }
  }
  
  var json_entries = JSON.stringify(entries),
      date = new Date;

  console.save(json_entries, "mfp-export-" + date.getTime());

})();

@@ -1,8 +1,31 @@
+(function(console)%7B%0A%0A  
 /* conso
@@ -64,16 +64,18 @@
 ippets %0A
+  
  * http:
@@ -129,33 +129,15 @@
 ave%0A
+  
  */%0A%0A
-(function(console)%7B%0A
 %0A  c
@@ -821,16 +821,17 @@
 console)
+;
 %0A%0A%0A(func
@@ -924,16 +924,311 @@
 ildren()
+,%0A      months = %7B%22January%22: 1,%0A          %22February%22: 2,%0A          %22March%22: 3,%0A          %22April%22: 4,%0A          %22May%22: 5,%0A          %22June%22: 6,%0A          %22July%22: 7,%0A          %22August%22: 8,%0A          %22September%22: 9,%0A          %22October%22: 10,%0A          %22November%22: 11,%0A          %22December%22: 12%0A      %7D
 ;%0A%0A  fun
@@ -1295,63 +1295,95 @@
 ' ')
-; %0A
+,
 %0A    
-return %7B
+    monthName = date%5B0%5D,
 %0A      
-'
+  
 month
-': 
+ = months%5B
 date%5B0%5D
+%5D
 ,
- 
 %0A      
-'day': 
+  day = parseInt(
 date
@@ -1407,32 +1407,232 @@
 ,'')
-, %0A      'year': date%5B2%5D
+),%0A        year = parseInt(date%5B2%5D),%0A        dateObject = new Date(year, month, day);%0A%0A    return %7B%0A      'month': month, %0A      'monthName': monthName,%0A      'day': day, %0A      'year': year,%0A      'ts': dateObject.getTime()
 %0A   
@@ -2200,16 +2200,86 @@
 te.year;
+%0A        entry.monthName = date.monthName;%0A        entry.ts = date.ts;
 %0A%0A      

Add timestamp and monthName fields to exported data