本文是小编为大家收集整理的关于从PhantomJS调用injectionJS暴露变量的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我已经按照示例从"入门"页面注入jQuery,这很好.我在同一目录中有一个本地jQuery副本,然后做类似...
之类的事情if(page.injectJs('jquery.min.js')) { page.evaluate(function(){ //Use jQuery or $ } }
当我尝试注入自己的脚本时,这些功能都无法使用.说我有一个叫MyScript.js的脚本,
function doSomething() { // doing something... }
我不能使用...
这样的滴滴if(page.injectJs('myScript.js')) { console.log('myScript injected... I think'); page.evaluate(function() { doSomething(); }); } else { console.log('Failed to inject myScript'); }
我尝试了
window.doSomething = function() {};
和
document.doSomething = function() {};
也没有运气,并尝试使用window.dosomething()或document.dodos.dosomething()在后续页面中拨打它们.
推荐答案
以下对我有效的作用,也许您的应用程序逻辑的其他部分是错误的:
inject.coffee
page = require('webpage').create() page.onConsoleMessage = (msg) -> console.log msg page.open "http://www.phantomjs.org", (status) -> if status is "success" page.includeJs "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", -> if page.injectJs "do.js" page.evaluate -> title = echoAndReturnTitle('hello') console.log title phantom.exit()
do.coffee:
window.echoAndReturnTitle = (arg) -> console.log "echoing '#{arg}'" console.log $(".explanation").text() return document.title
结果:
> phantomjs inject.coffee echoing 'hello' PhantomJS is a headless WebKit with JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. PhantomJS is created by Ariya Hidayat. PhantomJS: Headless WebKit with JavaScript API
或者您喜欢JavaScript(它们是自动生成的,有点丑):
`impt.js':
// Generated by CoffeeScript 1.3.1 (function() { var page; page = require('webpage').create(); page.onConsoleMessage = function(msg) { return console.log(msg); }; page.open("http://www.phantomjs.org", function(status) { if (status === "success") { return page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", function() { if (page.injectJs("do.js")) { page.evaluate(function() { var title; title = echoAndReturnTitle('hello'); return console.log(title); }); return phantom.exit(); } }); } }); }).call(this);
do.js:
// Generated by CoffeeScript 1.3.1 (function() { window.echoAndReturnTitle = function(arg) { console.log("echoing '" + arg + "'"); console.log($(".explanation").text()); return document.title; }; }).call(this);
问题描述
I've followed examples for injecting jQuery from the getting started page and that works just fine. I have a local copy of jQuery in the same directory, and do something like...
if(page.injectJs('jquery.min.js')) { page.evaluate(function(){ //Use jQuery or $ } }
When I try to inject my own script(s), none of the functions are available to me. Say I have a script called myScript.js that just has
function doSomething() { // doing something... }
I cannot then use doSomething like...
if(page.injectJs('myScript.js')) { console.log('myScript injected... I think'); page.evaluate(function() { doSomething(); }); } else { console.log('Failed to inject myScript'); }
I've tried
window.doSomething = function() {};
and
document.doSomething = function() {};
as well with no luck, as well as trying to call them with window.doSomething() or document.doSomething() in the subsequent page.evaluate().
推荐答案
The following works for me, maybe some other part of your app logic is wrong:
inject.coffee
page = require('webpage').create() page.onConsoleMessage = (msg) -> console.log msg page.open "http://www.phantomjs.org", (status) -> if status is "success" page.includeJs "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", -> if page.injectJs "do.js" page.evaluate -> title = echoAndReturnTitle('hello') console.log title phantom.exit()
do.coffee:
window.echoAndReturnTitle = (arg) -> console.log "echoing '#{arg}'" console.log $(".explanation").text() return document.title
Result:
> phantomjs inject.coffee echoing 'hello' PhantomJS is a headless WebKit with JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. PhantomJS is created by Ariya Hidayat. PhantomJS: Headless WebKit with JavaScript API
or if you prefer JavaScript (they're auto-generated and a little ugly):
`inject.js':
// Generated by CoffeeScript 1.3.1 (function() { var page; page = require('webpage').create(); page.onConsoleMessage = function(msg) { return console.log(msg); }; page.open("http://www.phantomjs.org", function(status) { if (status === "success") { return page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", function() { if (page.injectJs("do.js")) { page.evaluate(function() { var title; title = echoAndReturnTitle('hello'); return console.log(title); }); return phantom.exit(); } }); } }); }).call(this);
do.js:
// Generated by CoffeeScript 1.3.1 (function() { window.echoAndReturnTitle = function(arg) { console.log("echoing '" + arg + "'"); console.log($(".explanation").text()); return document.title; }; }).call(this);