简体   繁体   English

将全局变量从一个评估页面传递到另一个

[英]Passing Global Variable from One Evaluated Page to Another

So I have some code and I would like to pass a variable outside so I can evaluate another page and inject the code from the previous page. 所以我有一些代码,我想在外面传递一个变量,所以我可以评估另一个页面并注入上一页的代码。

I'm not an expert here and I'm just not grasping the concept. 我不是这里的专家,我只是没有理解这个概念。 Can anyone help me understand what I'm doing wrong? 谁能帮我理解我做错了什么?

var scheduleArray = [];

//blah blah removed code...everything works up to this point

casper.thenEvaluate(function(scheduleArray){

    console.log("##Your schedule is " + document.querySelector('form + div table').textContent );
    var rawSchedule = document.querySelector('form + div table').textContent;
    scheduleArray = rawSchedule.match(/((Monday)|(Tuesday)|(Wednesday)|(Thursday)|(Friday)|(Saturday)|(Sunday))([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4})((5)|(C6)|(6)|(7H)|(7F)|(715)|(8F)|(10F)|(12F)|(1F)|(2H)|(C2)|(2))/gi);
    console.log("##scheduleArray");
    console.log(scheduleArray);

    for (i=0;i<scheduleArray.length;i++){
        console.log(scheduleArray[i]);
    }


},scheduleArray);

casper.then(function(scheduleArray){
    console.log("##scheduleArray");
//This loop contains no data
    for (i=0;i<scheduleArray.length;i++){
            console.log(scheduleArray[i]);
        }
},scheduleArray);

I write a small sample code for you to illustrate how to passing result between evaluate and the casper script: 我为您编写了一个小示例代码,以说明如何在evaluate和casper脚本之间传递结果:

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

var array = []

casper.start('http://www.ecma-international.org/memento/TC39.htm');

casper.then(function() {
    array = casper.evaluate(function () {
        var nodes = document.querySelectorAll('a')
        var result = Array.prototype.map.call(nodes, function (div) {
            return div.href
        })
        return result
    });
});

casper.then(function () {
    casper.echo(array.length);
    casper.echo(array.join("\n"))
})

casper.run()

Output: 输出:

22
http://www.ecma-international.org/default.htm
http://www.ecma-international.org/contact/contact.html
http://www.ecma-international.org/sitemap/ecma_sitemap.html
http://www.ecma-international.org/memento/index.html
http://www.ecma-international.org/activities/index.html
http://www.ecma-international.org/news/index.html
http://www.ecma-international.org/publications/index.html
http://www.ecma-international.org/memento/history.htm
... ignore some lines

So, come to your code: 所以,来你的代码:

  1. The change of the argument of evaluate doesn't take that change to your global variable. evaluate参数的更改不会将更改转换为全局变量。 That is to say, what ever you do to scheduleArray inside evaluate , the global variable scheduleArray remains the same. 也就是说,无论你对scheduleArray里面的evaluate做什么,全局变量scheduleArray保持不变。

  2. Inside evaluate , you should use console.log to log, but outside the evaluate , you should use casper.echo . evaluate ,您应该使用console.log进行记录,但在evaluate之外,您应该使用casper.echo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM