14

I have captured list of data from the page using Greasemonkey.

GM Script

var hit = GM_getValue("hit") || 0;
var _url = "http://localhost:8080/test?p=$$pageNo$$";
_url = _url.replace("$$pageNo$$", hit);
GM_setValue("hit", ++hit); 
if(hit <= 100) {
window.location.href = _url;
}

This script will runs for nth time and capture <10K data, now i facing the issue in storing the captured data in some file. Anyone has any idea about how we can store the captured data into file/repo?

Thanks - Viswanathan G

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
iamjustcoder
  • 4,714
  • 10
  • 33
  • 46
  • possible duplicate of [Save File using Greasemonkey](http://stackoverflow.com/questions/2951149/save-file-using-greasemonkey) – Brock Adams Jun 18 '11 at 00:00

4 Answers4

19

A very fast and easy solution is to use FileSaver.js :
1) Add the following line into the ==UserScript== section of your Greasemonkey script

// @require     https://raw.githubusercontent.com/eligrey/FileSaver.js/master/dist/FileSaver.min.js
  1. Add the 2 following lines of code to the GM script

    var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});

    saveAs(blob, "hello world.txt");
    This code example will display a dialog box to download a file named "hello world.txt" containing the text "Hello, world!". Just replace this by the file name and the text content of your choice !

Nabi K.A.Z.
  • 9,887
  • 6
  • 59
  • 81
Ismael EL ATIFI
  • 1,939
  • 20
  • 16
  • Nice idea! The current `require`has to be set for the URL `https://raw.githubusercontent.com/eligrey/FileSaver.js/master/src/FileSaver.js`. – Francesco Marchetti-Stasi Feb 27 '19 at 23:27
  • 4
    Same result, but you'd better get `// @require https://raw.githubusercontent.com/eligrey/FileSaver.js/master/dist/FileSaver.js` – MrMartin Mar 28 '19 at 11:48
14

Nope, can't write it to a file, but if you're really bored, you can post it to http://pastebin.com (or any other URL that accepts a POST request with a bunch of data).

GM_xmlhttpRequest({
  method: "POST",
  url: "http://pastebin.com/post.php",
  data: <your data here>,
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  onload: function(response) {
    alert("posted");
  }
});

Note you need to have a pastebin account to use the API.


If you really need to write a file to your local filesystem, run a web server on your desktop, and then save the results of an http PUT request to disk.

ironchefpython
  • 3,409
  • 1
  • 19
  • 32
  • Its pretty good idea. But if i am using xyz.com, is possible to send xmlHttpRequest in cross domain? – iamjustcoder Jun 18 '11 at 00:22
  • 1
    @gviswanathan Notice the `GM_xmlhttpRequest` object I'm using? That's the magic that lets me send data **anywhere**. – ironchefpython Jun 18 '11 at 01:22
  • > run a web server on your desktop, and then save the results of an http PUT request to disk. Can it be explained in more details on how to do this? How exactly I need to run a web server? And how the "upload" need to be done to that server? – Ashark Jun 04 '21 at 15:15
7

I use this trick to download a file from a Tampermonkey script:

var saveData = (function () {
    var a = document.createElement("a");
    document.body.appendChild(a);
    a.style = "display: none";
    return function (data, fileName) {
        var blob = new Blob([data], {type: "octet/stream"});
        var url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = fileName;
        a.click();
        window.URL.revokeObjectURL(url);
    };
}());

Then call it with:

saveData("this data will be written in the file", "file.txt");

It works by creating a hidden element and simulating that element being clicked. It will behave as if the user clicked a download link, so the file will be downloaded by the browser, and saved wherever the browser puts downloaded files.

foolo
  • 804
  • 12
  • 22
0

Yes, you can write to a file. But not everywhere in the system, for obvious security reasons, you can just write in cookies directory

  var cookie_name="YourCookie";
  var cookie_value="What you want to save inside your cookie";
  var d = new Date();
  d.setTime(d.getTime() + (28*24*60*60*1000));
  var expires = "expires="+ d.toUTCString();
  document.cookie = cookie_name +"=" + cookie_value + ";" + expires + ";path=/";

You can then

  • script a filecopy from cookie directory to your desktop, depending on your OS

  • or read value from Chrome Inspect -> Application -> Cookies

  • or retrieving the Cookie and print it in console with

    decodeURIComponent(document.cookie);

Dharman
  • 30,962
  • 25
  • 85
  • 135