使用 JavaScript/GreaseMonkey 存储到文件中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6392103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing into file using JavaScript/GreaseMonkey
提问by iamjustcoder
I have captured list of data from the page using Greasemonkey.
我已经使用 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?
该脚本将运行第 n 次并捕获 <10K 数据,现在我面临将捕获的数据存储在某个文件中的问题。任何人都知道我们如何将捕获的数据存储到文件/存储库中?
Thanks - Viswanathan G
谢谢 - 维斯瓦纳坦 G
回答by ironchefpython
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).
不,不能将它写入文件,但是如果您真的很无聊,可以将它发布到http://pastebin.com(或任何其他接受带有一堆数据的 POST 请求的 URL)。
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.
请注意,您需要有一个 pastebin 帐户才能使用该 API。
If you reallyneed 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.
如果您确实需要将文件写入本地文件系统,请在桌面上运行 Web 服务器,然后将 http PUT 请求的结果保存到磁盘。
回答by Ismael EL ATIFI
A very fast and easy solution is to use FileSaver.js:
1) Add the following line into the ==UserScript== section of your Greasemonkey script
一个非常快速和简单的解决方案是使用FileSaver.js:
1) 将以下行添加到 Greasemonkey 脚本的 ==UserScript== 部分
// @require https://raw.githubusercontent.com/eligrey/FileSaver.js/master/FileSaver.js
2) Add the 2 following lines of code to the GM script
2)在GM脚本中加入以下2行代码
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 !
此代码示例将显示一个对话框,用于下载名为“hello world.txt”的文件,其中包含文本“Hello, world!”。只需将其替换为您选择的文件名和文本内容即可!