user4853
user4853

Reputation: 363

Using PhantomJS w/ Node and Setting Custom UserAgent

I am having trouble setting a custom user-agent for my phantom page. I have searched for possible solutions but I seem to be missing some fundamental part of how this should be working because when I try to set my settings, my phantom just hangs and doesn't complete the request or move into the page.open method. Here is my code:

phantom.create().then(function(ph) {
     ph.createPage().then(function(page) {
       page.set('settings.userAgent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.256');
       page.open(req.cookies.website).then(function(status) {
         page.property('content').then(function(content) {
           res.send(content);
           page.close();
           ph.exit();
         });
       });
     });
   });

Upvotes: 2

Views: 945

Answers (1)

user4853
user4853

Reputation: 363

In case anyone is wondering, I solved it... I just needed to look more closely at the phantom npm documentation. Here is the solution if anyone else has the same problem:

phantom.create().then(function(ph) {
     ph.createPage().then(function(page) {
       page.setting('userAgent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.256');
       page.open(req.cookies.website).then(function(status) {
         page.property('content').then(function(content) {
           res.send(content);
           page.close();
           ph.exit();
         });
       });
     });
   });

Upvotes: 3

Related Questions