javascript - CasperJS 绑定(bind)问题

标签 javascript phantomjs instagram casperjs

我正在尝试访问 Instagram 页面,但没有成功。我不断收到错误消息和空白屏幕截图。

错误文本:

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

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();
});

最佳答案

如果使用 PhantomJS 2,则不再需要下面的 shim。遗憾的是 CasperJS 1.1-beta3 还不支持它,所以你可能想使用来自 GitHub 的 master 分支。 .


问题是 PhantomJS v1.x 不支持 Function.prototype.bind。您需要为此添加垫片。在 CasperJS 中,它进入 page.initialized。事件处理程序。 This shim在 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;
        }
    });
});

如果 shim 被导出到它自己的文件中并通过 clientScripts 包含进来,它就不起作用了选项,因为这些附加在 instagram javascript 之后,为时已晚。

注册到 page.resource.received 也可能有用事件。

还有一个纯 PhantomJS 问题:bind polyfill for PhantomJS

关于javascript - CasperJS 绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25359247/

相关文章:

python - 如何让脚本等待一段时间,以便加载来自 ajax 响应的数据,然后捕获网页

instagram - 使用 Instagram Basic Display API 获取用户媒体

javascript - 使用 PhantomJS 进行单元测试时如何防止 img src 404 错误

javascript - 显示带有下拉按钮的现有 div?

javascript - 什么时候检查函数的参数是矫枉过正的?

javascript - 在 web 应用程序中实现 facebook 共享时 Error get The parameter app_id is required

javascript - 将参数数组从node.js传递给CasperJS

python - 使用文本文件中的行作为函数的数据

android - instagram 如何实现他们的登录屏幕?

javascript - 正确使用||