LucianoGanga / simple-headless-chrome

Simple abstraction to use Chrome as a Headless Browser with Node JS
MIT License
217 stars 50 forks source link

Can't set a specific areas to be screenshotted #54

Closed fusionx1 closed 7 years ago

fusionx1 commented 7 years ago

This one is more of a feature, I hope we will have the capability of setting up the specific section in the page to be screenshotted.

LucianoGanga commented 7 years ago

I think that one is a hard feature... haha

From the 'user' point of view. How would you like to define the section to be screenshotted?

LucianoGanga commented 7 years ago

Well, you got me thinking.. And as I had to make some fixes in the screenshot methods, I made this for you :)

https://github.com/LucianoGanga/simple-headless-chrome/blob/master/examples/simple-headless-chrome-files.png

https://github.com/LucianoGanga/simple-headless-chrome/blob/master/examples/screenCapture.js

I hope it helps!

Oh, and in that case you only have to pass a selector, but you can also pass a property called "clip", with a Viewport object, so you can manually select the area that you want to screenshot.

Besides that, there's a method called getSelectorViewport() where you can obtain the viewport of a element in a selector. That's useful if you want to, let's say, combine 2 elements and make the viewport the sum of both.

An example of the "clip" parameter may be:

    await mainTab.saveScreenshot('./examples/simple-headless-chrome-clip', {
      clip: {
              x: 350, // X coordinate reference point
              y: 700, // Y coordinate reference point
              width: this.options.deviceMetrics.width, // Width of the screenshot
              height: this.options.deviceMetrics.height, // Height of the screenshot
              scale: this.options.deviceMetrics.deviceScaleFactor // Scale (I don't understand it yet 😅)
      }        
    })

Please let me know if there's a problem with this, but the example worked for me.

Cheers! Lucho

fusionx1 commented 7 years ago

Hi Luciano,

Thank you so much for providing this feature. Ive been trying to make this work for the last 2 weeks but i always failed, even using 4.3.1 or 4.3.2