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=$$page开发者_Go百科No$$";
_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
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
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 !
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.
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.
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);
精彩评论