3

I am trying to add bind polyfill in my phantomjs file using es5-shim.js.

I have tried to include es5-shim.js using require(), but I am still getting error when I execute the phantomjs file. What is the correct way to use this?

Erwin
  • 1,762
  • 3
  • 24
  • 30
  • Check [this thread](https://github.com/ariya/phantomjs/issues/10522) there are few suggestions on polyfills you can use to handle it. – Mushegh A. Jun 20 '14 at 11:39
  • I required it simply with `require('./lib/es5-shim.js');` and it worked for me. The shim I used can be found [here](https://github.com/es-shims/es5-shim/blob/master/es5-shim.js) – Daniel Apt Jan 28 '15 at 09:54

1 Answers1

1

I tried the polyfill in this link and it seems to work fine for me.

Note that phantomjs scripts run inside WebKit's JavaScriptCore engine (which uses ES5) so most of these functions should already come out of the box.

D:\>phantomjs.exe

phantomjs> console.log(Object.keys)
function keys() {
    [native code]
}
undefined

phantomjs> var shim = require("D:\\es5-shim.js");
undefined

phantomjs> console.log(Object.keys)
function keys(object) {
        if (isArguments(object)) {
            return originalKeys(ArrayPrototype.slice.call(object));
        } else {
            return originalKeys(object);
        }
    }
Steven de Salas
  • 20,944
  • 9
  • 74
  • 82
  • Thanks for pointing me to the link of that shim! You're an absolute live safer! – Daniel Apt Jan 28 '15 at 09:52
  • hey @steven, or anyone: i'm implementing phantomJS through selenium webdriver with python bindings. how can i add this shim so that it will be included for the entire session of my PJS webriver instance? my webdriver instance does not have a "require" method, and it's tough to google it :-). this does not seem to merit a new SO question, but it's frustrating to me that i'm so close to solving this problem but can't seem to implement the solution. – Magenta Nova Feb 10 '15 at 16:45
  • I'm not familiar enough with [GhostDriver](https://github.com/detro/ghostdriver) but my guess is that since this running the webdriver protocol designed by Selenium (well before PhantomJS), there is no interaction with PhantomJS embedded JavaScriptCore engine and all commands go straight to the Webkit instance acting as the browser. – Steven de Salas Feb 10 '15 at 23:50
  • i think i follow, but it's still not clear to me how/where i insert the shim. please help. – Magenta Nova Feb 11 '15 at 17:26
  • I'm basically saying you cant unless you use PhantomJS directly via the command line. – Steven de Salas Feb 11 '15 at 23:11