Saving to a file using JavaScript / GreaseMonkey

I grabbed a list of data from a 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 work for the nth time and will collect data for 10K, now I ran into a problem when storing the captured data in any file. Does anyone know how we can store captured data in a file / repo?

Thanks - Viswanathan G

+6
source share
2 answers

No, I can not write it to a file, but if you are really bored, you can send it to http://pastebin.com (or any other URL that accepts a POST request with a lot 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"); } }); 

Please note that you need to have a pastebin account to use the API.


If you really need to write the file to the local file system, start the web server on your desktop and save the results of the http PUT request to disk.

+10
source

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

 // @require https://raw.githubusercontent.com/eligrey/FileSaver.js/master/FileSaver.js 

2) Add the following two lines of code in the GM script

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


This sample code will display a dialog box for downloading a file called "hello world.txt" containing the text "Hello, world!" Just replace it with the file name and text content of your choice!

+5
source

Source: https://habr.com/ru/post/890792/


All Articles