2014-12-26 11 views
6

Sì, lo so. Il binding delle funzioni non è supportato da Phantomjs. Ma forse posso usare qualcos'altro, o dire page.open di non usare bind? Sembra essere OK, ma alcuni siti web tornare errorePhantomjs Function.prototype.bind

TypeError: 'undefined' is not a function (evaluating 'b.bind(a)') 

Dopo che ho scritto un semplice script, che appena si apre una pagina:

var address = phantom.args[0]; 
if(!address) phantom.exit(1); 

page = require("webpage").create(); 

page.open(address, function(status){ 
setInterval(
    function() { 

    console.log(
     page.evaluate(function() { 
      return document.body.innerHTML.length; 
     }) 
    )}, 200) 
}) 

Ma l'errore è ancora lì. L'errore non è il problema principale, ma il problema è quello di ottenere il contenuto della pagina, perché dopo il contenuto della pagina di errore non viene caricato ...

Quindi, ho bisogno di aiuto.

P.S. Il sito Web del problema è http://vkrushelnytskiy.wix.com/aaaa

+0

Perché pensi che la pagina venga caricata come non è successo nulla quando l'errore di collegamento proviene da 'require.min.js'? –

+1

possibile duplicato di [Phantomjs page.content non sta recuperando il contenuto della pagina] (http://stackoverflow.com/questions/26382041/phantomjs-page-content-isnt-retrieving-the-page-content) –

+0

^Ho provato fuori con il tuo URL e funziona. –

risposta

1

È possibile eseguire il shim Function.bind utilizzando il seguente polyfill.

Basta anteporre il codice al codice che si sta tentando di eseguire. Probabilmente ci sono soluzioni migliori, ma per me ha funzionato alla grande.

+1

Questo non funziona qui. Non chiedermi il perché. –

2
You can use this code as an alternative 

if (!Function.prototype.bind) { 
    Function.prototype.bind = function(oThis) { 
    if (typeof this !== 'function') { 
     // closest thing possible to the ECMAScript 5 
     // internal IsCallable function 
     throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); 
    } 

    var aArgs = Array.prototype.slice.call(arguments, 1), 
     fToBind = this, 
     fNOP = function() {}, 
     fBound = function() { 
      return fToBind.apply(this instanceof fNOP 
       ? this 
       : oThis, 
       aArgs.concat(Array.prototype.slice.call(arguments))); 
     }; 

    fNOP.prototype = this.prototype; 
    fBound.prototype = new fNOP(); 

    return fBound; 
    }; 
} 
1

È possibile citare questa funzione di collegamento prima del caso di test.

// PhantomJS doesn't support bind yet 
Function.prototype.bind = Function.prototype.bind || 
    function (ctx) { 
    var fn = this, 
     args = [], 
     param_length = 0; 

    for(var i=0; i<arguments.length; i++) {  
    if(i){ 
     args[i-1] = arguments[i]; 
    } 
    } 
    param_length = args.length; 
    return function() { 
    for(var i =0; i<arguments.length; i++){ 
     args[param_length + i] = arguments[i]; 
    } 
    return fn.apply(ctx, args); 
    }; 
}; 
7

C'è un npm package which loads a polyfill for phantomJS's missing .bind method. Copia qui le istruzioni di installazione per comodità, ma segui il link per eventuali aggiornamenti.

installazione

npm install --save-dev phantomjs-polyfill 

Uso

require('phantomjs-polyfill') 

Uso con il Karma Calcola l'ovatta direttamente nella lista dei file del vostro karma.conf

... 
files: [ 
    './node_modules/phantomjs-polyfill/bind-polyfill.js', 
    ... 
] 
...