Phantomjs Function.prototype.bind

Yeah, I know. The bind function is not supported by Phantomjs. But maybe I can use something else or tell page.open not to use bind ? Everything seems to be in order, but some sites return error

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

After that, I wrote a simple script that simply opens the page:

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

But the error still exists. The error is not the main problem, but the problem is getting the contents of the page, because after the contents of the error page does not load ...

So I need some help.

PS Problem Website http://vkrushelnytskiy.wix.com/aaaa

+6
source share
4 answers

There is an npm package that loads polyfill for the missing phantomJS.bind method . Copy the installation instructions here for convenience, but follow the link for any updates.

Installation

 npm install --save-dev phantomjs-polyfill 

Using

 require('phantomjs-polyfill') 

Use with karma Include polyfill directly in the list of files in your karma.conf

 ... files: [ './node_modules/phantomjs-polyfill/bind-polyfill.js', ... ] ... 
+7
source
 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; }; } 
+2
source

You can pin the Function.bind function using the following polyfill .

Just add it to the code you are trying to run. There are probably more pleasant solutions, but this is perfect for me.

+1
source

You can mention this binding function just before the test case.

 // 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); }; }; 
+1
source

Source: https://habr.com/ru/post/980178/


All Articles