从phantomjs沙盒环境中写入文件系统 [英] Writing to filesystem from within phantomjs sandboxed environment
问题描述
我需要遍历站点上的表单并将中间结果保存到文件中.我正在使用phantomjs的page.evaluate
,但是在从page.evaluate
的沙盒环境中访问文件系统时遇到问题.我有这样的东西:
I need to traverse forms on a site and save intermediate results to files. I'm using phantomjs' page.evaluate
, but I'm having trouble accessing the filesystem from within page.evaluate
's sandboxed environment. I have something like this:
for (var i = 0; i<option1.length; i++){
for (var ii = 0; ii<option2.length; ii++){
for (var iii = 0; iii<option3.length; iii++){
...
//I found what I want to save
fs.write("someFileName", someData);
}
}
}
很显然,我无法从page.evaluate
内部访问nodejs的fs
,因此上述操作无效.我似乎有几种选择:
Obviously, I don't have access to nodejs' fs
from within page.evaluate
, so the above does not work. I seem to have a few options:
- 将我需要写入的所有内容存储到数组中,然后将其从
page.evaluate
上下文返回到外部的nodejs上下文中,然后从那里保存它.这将需要我没有的内存. - 将以上逻辑分解为较小的
page.evaluate
方法,这些方法返回单行数据以保存到文件系统. - 以某种方式将<魔术>函数传递给
page.evaluate
以写入文件系统.这似乎是不可能的(例如,如果我尝试传入调用fs.writeFile
的函数,即使fs
是我传递的函数中的自由变量,我也会得到fs
是未定义的?) - 返回一个迭代器,当该迭代器被拉出时,将产生下一个要写入的数据
- 在本地主机上设置一个简单的Web服务器,该服务器仅接受POST请求并将其内容写入文件系统.然后,
page.evaluate
代码将向本地主机发出这些请求.我几乎会尝试这样做,但不确定是否会受到原产地政策的影响.
- Store everything I need to write to an array, and return that from the
page.evaluate
context into the outer, nodejs context, then save it from there. This would require memory I don't have. - Break up the above logic into smaller
page.evaluate
methods that return singe pieces of data to save to the filesytem. - Somehow pass into the
page.evaluate
a magic function to write to the filesystem. This seems to not be possible (if I try to pass in a function that callsfs.writeFile
for example, I get thatfs
is undefined, even iffs
is a free variable in the function I passed?) - Return an iterator which, when pulled, yields the next piece of data to be written
- Setup a trivial web server on the localhost that simply accepts POST requests and writes their contents into the filesystem. The
page.evaluate
code would then make those requests to the localhost. I almost try this but I'm not sure I'll be affected by the same-origin policy.
我在这里有什么选择?
推荐答案
Your evaluation is sound, but you forgot one type: onCallback
. You can register to the event handler in the phantom context and push your data from page context to a file through this callback:
page.onCallback = function(data) {
if (!data.file) {
data.file = "defaultFilename.txt";
}
if (!data.mode) {
data.mode = "w";
}
fs.write(data.file, data.str, data.mode);
};
...
page.evaluate(function(){
for (var i = 0; i<option1.length; i++){
for (var ii = 0; ii<option2.length; ii++){
for (var iii = 0; iii<option3.length; iii++){
...
// save data
if (typeof window.callPhantom === 'function') {
window.callPhantom({ file: "someFileName", str: someData, mode: "a" }); // append
}
}
}
}
});
请注意,PhantomJS不在Node.js中运行.虽然,Node.js和PhantomJS之间存在桥梁.另请在此处.
Note that PhantomJS does not run in Node.js. Although, there are bridges between Node.js and PhantomJS. See also my answer here.
这篇关于从phantomjs沙盒环境中写入文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!