注射&使用Firebug-Lite和PhantomJS [英] Injecting & using Firebug-Lite with PhantomJS
问题描述
我正在尝试注射Firebug& jQuery使用PhantomJS进入网页,但我无法访问它们。
I'm trying to inject Firebug & jQuery into a webpage using PhantomJS, but I'm unable to access them.
我已经尝试了两种PhantomJS方法将其他JS添加到页面:page.injectJs( )和page.includeJs。
I've tried both PhantomJS methods for adding other JS to a page: page.injectJs() and page.includeJs.
我无法从includeJs中获取任何内容(通过阅读文档我不希望得到)。
I'm not able to get anything returned from includeJs (which I wasn't expecting to get, from reading the documentation).
在我注入了js()并尝试使用firebug-lite和jQuery函数或对象(比如$和inspect())后,我得到的错误是它们未定义或变量无法找到。
After I injectJs() and try to use firebug-lite and jQuery functions or objects (like $ and inspect() ) I get errors saying they are undefined or the variable can't be found.
这是我的完整脚本。你也可以在这里看到它: http://piratepad.net/XTPefXOB4o
This is my complete script. You can also see it here: http://piratepad.net/XTPefXOB4o
"use strict";
"use warnings";
var page = new WebPage(), address;
var useragent = "PhantomJS Firebug integration tool (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11";
page.onConsoleMessage = function (msg) {
console.log("+eval:" + msg);
};
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
console.log("got into page.open()");
var testInclude = page.includeJs("http://getfirebug.com/releases/lite/1.4/firebug-lite.js", function () {
var result = true;
try {
inspect($("*")[5]);
}
catch(err) {
result = false;
}
console.log("from includeJS: " + result);
return result;
// is there any way to return a value from this?
});
var results = page.evaluate( function () {
var debug = [];
try {
page.injectJs("http://getfirebug.com/releases/lite/1.4/firebug-lite.js");
page.injectJs("http://pconerly.webfactional.com/site_media/js/jquery-1.7.min.js");
} catch(err) {
debug[0] = false;
}
debug[0] = true;
debug[1] = true;
try {
// all 3 of these commands fail.
//$(document);
inspect(document.querySelectorAll("*")[i]);
//firebug;
}
catch(err) {
console.log(err.message)
debug[1] = false;
}
//debug[1] = inspectAEl(5);
return debug;
});
var stuff = results;
console.log("did not error on injecting JS: " + stuff[0]);
console.log("used inspect within page.evaluate: " + stuff[1]);
console.log("return value from includeJs: " + testInclude);
console.log("processing finished");
}
phantom.exit();
});
这是我的输出:
$ phantomjs firebug-integration.js
got into page.open()
+eval:Can't find variable: inspect
did not error on injecting JS: true
used inspect within page.evaluate: false
return value from includeJs: undefined
processing finished
推荐答案
围绕每个函数的上下文你有一些困惑:
You've got some confusion here around the context of each function:
-
当你运行
includeJs(url,callback)
时,回调
函数在PhantomJS上下文。因此它可以访问页面
,但不可以访问由包含的脚本创建的变量和名称空间 - 这些包含在中客户端上下文,将通过page.evaluate()
运行的函数可用。
When you run
includeJs(url, callback)
, thecallback
function runs in the PhantomJS context. So it has access topage
, but does not have access to variables and namespaces created by the included script - these were included in the client context, and will be available to functions you run throughpage.evaluate()
.
您正尝试在 page.evaluate()
中运行 page.injectJs()
。这不起作用 - 您在 page.evaluate()
中运行的函数在客户端执行上下文中被沙箱化,并且无法访问 page
object。
You're trying to run page.injectJs()
within page.evaluate()
. This won't work - the function you run in page.evaluate()
is sandboxed in the client execution context, and does not have access to the page
object.
所以你应该尝试其中一种方法 - 运行 page.evaluate()
在 page.includeJs()
回调:
So you should try one of these approaches - either run page.evaluate()
in the page.includeJs()
callback:
page.includeJs("http://getfirebug.com/releases/lite/1.4/firebug-lite.js",
function () {
page.evaluate(function() {
// do stuff with firebug lite here
console.log(inspect($("*")[5]));
});
});
或运行 page.injectJs()
和然后运行 page.evaluate()
:
// note - this is a reference to a local file
page.injectJs("firebug-lite.js");
page.evaluate(function() {
// do stuff with firebug lite here
console.log(inspect($("*")[5]));
});
这篇关于注射&使用Firebug-Lite和PhantomJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!