在PhantomJS中注入Jquery

时间:2014-03-14 12:38:43

标签: javascript jquery phantomjs

我在使用phantomJS代码注入Jquery时遇到了问题。

这是我的代码:

    var page = require('webpage').create();
    var reqUrl = http://www.example.com
    page.open(reqUrl, function (status) {
        if (status === 'success') {
            console.log('success');
        }
    });
    page.onLoadStarted = function(){
        page.injectJs('jquery-1.11.0.min.js')
    }
    page.onLoadFinished = function (status) {
        if (status === 'success') {
            page.onConsoleMessage = function (msg) {
                console.log(msg);
            };
            waitFor(function () {
                return page.evaluate(function () {
                  var element = document.getElementsByTagName('body');
//                console.log($("body").is(":visible"))  not working
//                return $("body").is(":visible"); not working
                    if (typeof(element) != 'undefined' && element != null) {
                        return true
                    }
                    else {
                        return false;
                    }
                })
            }, function () {
                var getData = page.evaluate(function () {
                    var imageArr = [];
                    var body = $('body')
                    console.log(JSON.stringify(body)); // returning null
                    var imageData = document.getElementsByTagName('img');
                    for(var i in imageData) {
                        imageArr.push(imageData[i].src);
                    }
                    return JSON.stringify({images: imageArr});
                });
                // console.log(getData.images);
                console.log(getData);
                response.write(getData);
                response.close();
                phantom.exit();
            });
        }
    }
});
function waitFor(testFx, onReady, timeOutMillis) {
    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 5000, //< Default Max Timout is 5s
        start = new Date().getTime(),
        condition = false,
        interval = setInterval(function () {
            if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) {
                // If not time-out yet and condition not yet fulfilled
                condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
            } else {
                if (!condition) {
                    // If condition still not fulfilled (timeout but condition is 'false')
                    console.log("'waitFor()' timeout");
                    phantom.exit(1);
                } else {
                    // Condition fulfilled (timeout and/or condition is 'true')
                    console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                    typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
                    clearInterval(interval); //< Stop this interval
                }
            }
        }, 1000); //< repeat check every 250ms
};

我想在evaluate函数中使用JQuery方法,但是使用任何像$(&#39; body&#39;)这样的选择器返回null。我应该怎么做才能注入JQuery?

0 个答案:

没有答案