Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save File using Greasemonkey

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...

like image 500
Kris Erickson Avatar asked Jun 01 '10 15:06

Kris Erickson


People also ask

How does Greasemonkey work?

GreaseMonkey is a Firefox extension that lets you run arbitrary Javascript code against selected web pages. What this means is that GreaseMonkey lets you change how other people's web pages look and how they function (but just in your own browser, of course).

How to use Greasemonkey scripts in Firefox?

Installing the Greasemonkey Extension. Click on the Firefox drop-down menu at the top left of the browser and select Add-ons. Type Greasemonkey into the add-ons search box at the top right of the browser. Find Greasemonkey in the list and click on Install.


3 Answers

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:

  • http://jsfiddle.net/UselessCode/qm5AG/
  • How to create .txt file using JavaScript / HTML5?

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.

like image 160
Rub Avatar answered Oct 24 '22 22:10

Rub


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

like image 30
zanetu Avatar answered Oct 24 '22 23:10

zanetu


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

like image 20
Ryley Avatar answered Oct 24 '22 21:10

Ryley