19

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? Any suggestions on where to look for a sample or some documentation on this kind of functionality?

Just to be clear, I don't want to write to the local file system (I know that is impossible in the sandbox), but present a downloadable file - which may also be impossible...

Kris Erickson
  • 32,972
  • 26
  • 117
  • 173

6 Answers6

12

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):

  1. GM (greasemonkey) script generates the content of the file
  2. GM passes it to the web page using sessionStorage.variable="...content.."
  3. script within page makes link visible and attach the content of the variable to the BLOB object.

You many need to stringify / parse the object.

  • contacts=JSON.parse(sessionStorage.contacts)
  • sessionStorage.contacts=JSON.stringify(contacts);

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.

Community
  • 1
  • 1
Rub
  • 1,399
  • 13
  • 27
12
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

zanetu
  • 3,420
  • 1
  • 20
  • 16
  • Combining this with [@Rub's answer](https://stackoverflow.com/a/33985544/315024) (by doing `a.href = makeTextFile(data, 'text/csv');` instead) was perfect. – Walf Oct 15 '20 at 03:28
3

Maybe you can't write it to a local CSV, but you might be able to write it to say a Google Spreadsheet?

Ryley
  • 20,826
  • 1
  • 68
  • 81
  • Interesting idea. I will definitely look into this, but may require a lot more work than I am willing to do for this and maybe it would be faster to make an actual Firefox add-on to gain access to the filesystem. – Kris Erickson Jun 02 '10 at 23:45
1

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)

loshmee
  • 11
  • 1
  • that's my solution, but i can't get over CORS, even though I'm using header("Access-Control-Allow-Origin: *") in my php endpoint – Berry Tsakala Feb 13 '19 at 08:57
0

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.

j3ff
  • 5,139
  • 7
  • 39
  • 50
Noumenon
  • 3,977
  • 4
  • 46
  • 61
0

Unfortunately not. http://wiki.greasespot.net/FAQ#Can_Greasemonkey_be_used_to_open_local_files.3F

Anton
  • 2,633
  • 1
  • 15
  • 7