使用CasperJS中的函数返回iframe内的链接 [英] Returning links inside iframe using a function in CasperJS

查看:145
本文介绍了使用CasperJS中的函数返回iframe内的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从iframe中获取链接并将它们作为函数结果返回,我的简化代码如下所示:

I am trying to get the links from inside an iframe and return them as a function result, my simplified code looks something like this:

var casper = require("casper").create({
    verbose: true,
    logLevel: "debug",
        webSecurityEnabled: false
});

var url = casper.cli.get(0);

casper.on('remote.message', function(msg) {
    this.echo(msg);
})

casper.start(url, function () {
    thelinks = getLinksFromIframes( casper );
    console.log("doesn't work:" + thelinks);
});

function getLinksFromIframes( context ) {
        var links = [];

        var iframes = context.evaluate( function() {
                var iframes = [];
                [].forEach.call(document.querySelectorAll("iframe"), function(iframe, i) { iframes.push( i ); });
                return iframes;
        });

        iframes.forEach( function( index ) {
            context.withFrame(index, function() {
                links = links.concat( this.getElementsAttribute( 'a', 'href' ) );
                console.log("works: " + links);
            });
        });

        return links;
}

casper.run(function() {
    console.log('done');
    this.exit();
});

问题是该函数没有返回任何内容,我只能读取里面的链接 withFrame ,我知道还有其他方法可以获取链接,但代码是这样的,因为它是一个更复杂的部分,将分析嵌套的iframe,以及内部的iframe数量iframes未知。有什么方法可以等待 withFrame 或者允许我返回链接作为函数结果的东西吗?

The problem is that the function doesn't return anything, I can only read the links var inside withFrame, i know there are other ways to get the links, but the code is this way because it part of something more complex that will analyze nested iframes, and the amount of iframes inside iframes is unknown. Is there any way I could wait on withFrame or something that will allow me to return the links as the function result?

推荐答案

这是预期的,因为 casper.withFrame 是一个异步步骤函数。与所有其他以开头,然后等待的函数一样,它会调度CasperJS执行队列中的一个步骤。

That's expected, because casper.withFrame is an asynchronous step function. Like all other functions that begin with either then or wait, it schedules a step in the CasperJS execution queue.

执行这些计划步骤时(在当前步骤结束时,然后回调 casper.start 在您的情况下), getLinksFromIframes 已经完成并返回一个空数组。

When those scheduled steps are executed (at the end of the current step, which is the then callback of casper.start in your case), getLinksFromIframes has long finished and returned an empty array.


我有没有办法等待withIframe或允许我返回链接作为函数结果的东西?

Is there any way I could wait on withIframe or something that will allow me to return the links as the function result?

不,但你可以使用回调:

No, but you can use a callback:

function getLinksFromIframes( callback ) {
    var links = [];

    var iframes = this.evaluate( function() {
        var iframes = [];
        [].forEach.call(document.querySelectorAll("iframe"), function(iframe, i) { iframes.push( i ); });
        return iframes;
    });

    iframes.forEach( function( index ) {
        this.withFrame(index, function() {
            links = links.concat( this.getElementsAttribute( 'a', 'href' ) );
            console.log("works: " + links);
        });
    }, this);

    this.then(function(){
        callback.call(this, links);
    });
}

casper.start(url, function () {
    getLinksFromIframes.call(this, function(links){
        thelinks = links;
        console.log("Links: " + thelinks);
    });
})
.then(function(){
    console.log("Links later: " + thelinks);
})
.run();

这篇关于使用CasperJS中的函数返回iframe内的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆