Storing into file using JavaScript/GreaseMonkey

 ̄綄美尐妖づ 提交于 2019-12-17 23:25:57

问题


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


回答1:


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.




回答2:


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/FileSaver.js

2) 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 !



来源:https://stackoverflow.com/questions/6392103/storing-into-file-using-javascript-greasemonkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!