I have some screen scraped tabular data that I want to export to a CSV file (currently I am just placing it in the clipboard), is there anyway to do this in Greasemonkey?
alternative approach could be if you fire a javascript controlled http request for each cvs line you have to a local http server applet that is capable of storing (simple cgi or apache/php could do it easy)
The free Javascript utility JSZip uses the blob approach to generate a Zip file that pops up for you to download. The user script Fitocracy Bulk CSV used JSZip to collect the 100 files of workout data it generated.
Maybe you can't write it to a local CSV, but you might be able to write it to say a Google Spreadsheet?
Unfortunately not. http://wiki.greasespot.net/FAQ#Can_Greasemonkey_be_used_to_open_local_files.3F
var data='col1,col2\nval1,val2';
var a = document.createElement('a');
a.href = 'data:application/csv;charset=utf-8,' + encodeURIComponent(data);
//supported by chrome 14+ and firefox 20+
a.download = 'data.csv';
//needed for firefox
document.getElementsByTagName('body')[0].appendChild(a);
//supported by chrome 20+ and firefox 5+
a.click();
DEMO
Yes you can do it using BLOB.
The script will attach content to a link that when clicked will offer to download a file (a file that never existed).
More info on:
This is how I did it (there are many other ways to do it):
You many need to stringify / parse the object.
I modified slightly the original script to make it generic for multiple mime types.
Here is mine.
// Stuff to create the BLOB object --- ANY TYPE ---
var textFile = null,
//-- Function
makeTextFile = function (text,textType) {
// textType can be 'text/html' 'text/vcard' 'text/txt' ...
var data = new Blob([text], {type: textType });
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
return textFile;
};
Hope it helps.