CasperJS绑定问题 [英] CasperJS bind issue

查看:146
本文介绍了CasperJS绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问Instagram页面,但没有运气。我一直收到错误和空白屏幕截图。

I'm trying to reach an instagram page, but with no luck. I keep getting the error and a blank screenshot.

错误文字:

TypeError: 'undefined' is not a function (evaluating 'a.createDescriptor.bind(null,t)')

Casperjs --version是1.1.0-beta3。

Casperjs --version is 1.1.0-beta3.

基本上我使用以下代码:

Basically I use the following code:

var casper = require('casper').create({
    verbose: true,
    logLevel: 'debug',
    pageSettings: {
         userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
    },
    loadPlugins: true
});

casper.on( 'page.error', function (msg, trace) {
    this.echo( 'Error: ' + msg, 'ERROR' );
});

casper.start('http://instagram.com/hello', function() {
    casper.wait(3000, function()  {
        this.capture('screen.png');
    });
});

casper.run(function() {
    this.exit();
});


推荐答案

如果PhantomJS 2不再需要垫片用来。可悲的是CasperJS 1.1-beta3还不支持它,所以你可能想要使用 GitHub

The shim below isn't needed anymore if PhantomJS 2 is used. Sadly CasperJS 1.1-beta3 doesn't support it yet, so you might want to use the master branch from GitHub.

问题是PhantomJS v1.x不支持 Function.prototype。结合。你需要为它添加垫片。在CasperJS中,它进入 页面。初始化 事件处理程序。 这个垫片对我来说非常适合:

The problem is that PhantomJS v1.x does not support the Function.prototype.bind. You need to add a shim for that. In CasperJS it goes into the page.initialized event handler. This shim works well for me on instragram:

casper.on( 'page.initialized', function(){
    this.evaluate(function(){
        var isFunction = function(o) {
          return typeof o == 'function';
        };

        var bind,
          slice = [].slice,
          proto = Function.prototype,
          featureMap;

        featureMap = {
          'function-bind': 'bind'
        };

        function has(feature) {
          var prop = featureMap[feature];
          return isFunction(proto[prop]);
        }

        // check for missing features
        if (!has('function-bind')) {
          // adapted from Mozilla Developer Network example at
          // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
          bind = function bind(obj) {
            var args = slice.call(arguments, 1),
              self = this,
              nop = function() {
              },
              bound = function() {
                return self.apply(this instanceof nop ? this : (obj || {}), args.concat(slice.call(arguments)));
              };
            nop.prototype = this.prototype || {}; // Firefox cries sometimes if prototype is undefined
            bound.prototype = new nop();
            return bound;
          };
          proto.bind = bind;
        }
    });
});

如果将填充程序导出到自己的文件并通过 clientScripts 选项,因为这些是在Instagram javascript之后附加的,为时已晚。

It doesn't work if the shim is exported into its own file and included through the clientScripts option, because those are appended after the instagram javascript which is too late.

注册 page.resource.received event。

What might also work is registering to page.resource.received event.

还有纯粹的PhantomJS问题:绑定polyfill for PhantomJS

There is also the pure PhantomJS question: bind polyfill for PhantomJS

这篇关于CasperJS绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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