CasperJS绑定问题

buz*_*ykg 11 javascript phantomjs instagram casperjs

我正试图到达一个Instagram页面,但没有运气.我一直收到错误和一个空白的截图.

错误文字:

TypeError: 'undefined' is not a function (evaluating 'a.createDescriptor.bind(null,t)')
Run Code Online (Sandbox Code Playgroud)

Casperjs --version是1.1.0-beta3.

基本上我使用以下代码:

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();
});
Run Code Online (Sandbox Code Playgroud)

Art*_* B. 20

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


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

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;
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

如果将填充程序导出到自己的文件并通过clientScripts选项包含它,则不起作用,因为这些是在Instagram javascript之后附加的,这太晚了.

注册page.resource.received活动也可能有用.

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