/*
    (c) 2014 Digital Humanities Lab, Faculty of Humanities, Utrecht University
    Author: Julian Gonggrijp, j.gonggrijp@uu.nl
    
    Automated text scraper for Spanish text corpora:
    http://corpus.rae.es/cordenet.html
    http://corpus.rae.es/creanet.html
    http://www.corpusdelespanol.org/
*/

(function ( ) {
    'use strict';

    var target = frames[6],
        doc = target.document,
        p = new DOMParser(),
        data = [];

    function insert_jquery_then (continuation) {
        var scriptnode = document.createElement('script');
        scriptnode.setAttribute('src', '//code.jquery.com/jquery-2.1.1.min.js');
        document.head.appendChild(scriptnode);
        scriptnode.addEventListener('load', continuation);
    }

    function cordelesp_turnpage_then (continuation, alternative) {
        var navtable = doc.querySelectorAll('#zabba table')[1];
        if (! navtable) {
            alternative();
            return;
        }
        var navrow = navtable.querySelectorAll('td')[2],
            anchor = navrow.querySelectorAll('a')[2],
            progress = navrow.childNodes[4].nodeValue.split('/'),
            table = doc.querySelector('#zabba');
        if (Number(progress[0]) < Number(progress[1])) {
            window.jQuery.get(anchor.href, function (data) {
                var result = p.parseFromString(data, 'text/html');
                table.innerHTML = result.querySelector('#zabba').innerHTML;
                continuation();
            });
        } else {
            alternative();
        }
    }

    function cordelesp_scrape1page ( ) {
        for (var i = 1; i <= 100; ++i) {
            var row = doc.querySelector('#t' + i);
            if (! row) continue;
            var anchors = row.querySelectorAll('a'),
                field = row.querySelector('#texto_' + i),
                rowdata = [];
            if (!anchors || !field || !field.value) continue;
            for (var j = 0; j < 3; ++j) {
                rowdata.push(anchors[j].childNodes[0].nodeValue.trim());
            }
            data.push(rowdata.concat(field.value.split(/<b><u>|<\/u><\/b>/)));
        }
    }

    function scrape_cordelesp ( ) {
        cordelesp_turnpage_then(function ( ) {
            data = data.concat(cordelesp_scrape1page());
            scrape_cordelesp();
        }, data2csv);
    }

    function data2csv ( ) {
        console.log(data);
        // step below removes mysterious undefined elements that
        // creep into the array
        data = data.filter(function (elem) { return elem; });
        var text = '';
        for (var l = data.length, i = 0; i < l; ++i) {
            var a = data[i];
            for (var m = a.length, j = 0; j < m; ++j) {
                text += a[j] + ';';
            }
            text += '\n';
        }
        document.write(
            'Scraping complete. Please copy the contents below ' +
            'into a plaintext document and give it a .csv extension.<br>' +
            '<textarea id="output" style="width: 50ex; height: 10em;">' +
            text +
            '</textarea>'
        );
        $('#output').focus().select();
    }

    cordelesp_scrape1page();
    insert_jquery_then(scrape_cordelesp);
})();

@@ -2668,17 +2668,17 @@
  data%5Bi%5D
-;
+,
 %0A       
@@ -2682,24 +2682,19 @@
         
-for (var
+   
  m = a.l
@@ -2698,17 +2698,38 @@
 a.length
-,
+;%0A            for (var
  j = 0; 
@@ -2731,16 +2731,25 @@
 = 0; j %3C
+ 2 && j %3C
  m; ++j)
@@ -2793,32 +2793,131 @@
 ;%0A            %7D%0A
+            for (var j = 2; j %3C m; ++j) %7B%0A                text += '%22' + a%5Bj%5D + '%22;';%0A            %7D%0A
             text
@@ -3173,24 +3173,95 @@
  10em;%22%3E' +%0A
+            'number;century;text;contextLeft;sample;contextRight;%5Cn' +%0A
             

Improve CSV export formatting