From what I can say, you will need to fix PhantomJS to achieve this.
PhantomJS contains the GhostDriver module that provides the HTTP API that WebDriver uses to communicate with the PhantomJS instance. So, everything you want to do through WebDriver should be supported by GhostDriver, but it does not look like onInitialized supported by GhostDriver.
If you feel like an adventurer, you can clone the PhantomJS repository and fix src / ghostdriver / session.js to do what you want.
The _init method is as follows:
_init = function() { var page;
You can try using the code you found:
page.onInitialized = function() { page.customHeaders = {}; };
in the page object created there.
Depending on what you are testing, although you can save a lot of effort and quit the browser and just test HTTP requests directly using something like the requests module.
source share