Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the user agent string in the phantom module?

var phantom = require('phantom');
console.dir(phantom);
phantom.create(function(browser){
    browser.createPage(function(page){
        page.customHeaders={
            "HTTP_USER_AGENT": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36",
            };
        console.dir(page.settings);
        //undefined
        page.settings={};
        page.settings.userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36';
        page.settings.HTTP_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36';
        console.dir(page.settings);
        page.open('http://example.com/req.php', function() {
            setTimeout(function() {
                var output = page.evaluate(function() {
                    return document;
                    });
                console.dir(output);
                //undefined
                }, 1000);
             });});});

when I use phantomjs I try and set the header for userAgent using three different ways but when I visit the page and save the PHP $_SERVER object to a txt pad I still see PhantomJS

HTTP_USER_AGENT: Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.0.1-development Safari/538.1

not only that but the output of the page is also undefined.

It seems that the docs have changed or I cant find the correct ones. I am looking at

http://phantomjs.org/api/webpage/property/settings.html

https://www.npmjs.com/package/phantom

How is this used correctly?

like image 645
Ben Muircroft Avatar asked Feb 17 '15 17:02

Ben Muircroft


1 Answers

According to the Functional Details in the docs, you have to set the user agent through page.set():

page.set('settings.userAgent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36');

It has to be done this way, because the bridge has to communicate with the PhantomJS process and isn't doing this in a non-asynchronous fashion. This could've probably been implemented with Object.defineProperty.

If you want to set multiple settings at once, you can do (ref):

page.set('settings', {
    userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11",
    javascriptEnabled: false,
    loadImages: false
});

You can find a list of settings that you can set in page.settings.

like image 50
Artjom B. Avatar answered Oct 27 '22 00:10

Artjom B.