0

I am using this code: var width = 1024; var height = 768;

and page.viewportSize = {width: width, height: height}; im using before page.open . But this doesn't work. The full code you can watch in my another question right here - Faking the Referer Header in PhantomJS is doesn't work

here is the code:

var width = 1024;
var height = 768;
var page = require('webpage').create();
//referal massive
var reff = ["https://www.facebook.com/messages/t/","https://google.com","https://youtube.com","https://twitter.com/"];
//random massive
var randreff = Math.floor(Math.random() * (reff.length));
//reffer
page.customHeaders = {
  "Referer": (reff[randreff])
};
//console refer
console.log(reff[randreff]);
var urls = ['http://test.com/','http://test.com/2017/02/blog-post.html','http://test.com/'];
var i = 0;
function OpenPage(){
    setTimeout(function(){
            page.open(urls[i], function(status) {
                    if (status == 'success') {
                page.viewportSize = {width: width, height: height};
                    page.render('example' + i + '.png');
            }
            i++;
            if(i <= urls.length - 1){ 
                OpenPage();
            }else{
               phantom.exit();
            }
        });
    },5000);
}
OpenPage();
  • Resolution doesn't work and referrals doesn't work =(
Community
  • 1
  • 1
Great Serg
  • 71
  • 1
  • 2
  • 10
  • Please post the full code here. The code from your other question is missing `page.viewportSize = {width: width, height: height};`. – subwaymatch Feb 26 '17 at 23:12
  • The full code indeed does matter. For example, code in one of the similar questions revealed the viewport size was set after page had already been opened - thus too late for it to work. – Vaviloff Feb 27 '17 at 05:05
  • I have edit my post and add the full code. page.viewportSize - doesn't work (in analytics I see only my PC screen resolution, not the resolution I have set in the code) and referrals doesn't work too....=( – Great Serg Feb 27 '17 at 14:01

1 Answers1

2

page.render() renders whole page regardless of viewport size, it's normal behaviour ... what you need is page.clipRect()... something like:

page.viewportSize = {width: width, height: height};

The above should be defined before page.open() - tells a page to render the way like the window would be of given size

To render image of given size, you need to:

page.clipRect = {
    top: 0,
    left: 0,
    height: viewportSize.height,
    width: viewportSize.width
  };

page.render(...);
Flash Thunder
  • 11,672
  • 8
  • 47
  • 91