Skip to content

Commit

Permalink
#7 added renderTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
werthdavid committed Mar 18, 2019
1 parent 928a0f8 commit 625720e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
5 changes: 3 additions & 2 deletions CameraSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ function Camera(hap, conf, log) {
Camera.prototype.handleSnapshotRequest = function (request, callback) {
let width = this.conf.width || (request.width * (this.conf.scale || 2));
let height = this.conf.height || (request.height * (this.conf.scale || 2));
let timeout = this.conf.timeout || 10000;
this.screenshotHelper.getScreenshot(width, height, timeout)
let networkTimeout = this.conf.timeout || 10000;
let renderTimeout = this.conf.renderTimeout || 1;
this.screenshotHelper.getScreenshot(width, height, networkTimeout, renderTimeout)
.then(
img => {
this.log("Got screenshot");
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ With e.g. `scale` set to `2` (default) the virtual browser window is set to this
* OR `width` / `height` the width/height of the virtual browser window. This is optional and overrides `scale`.
* `chromiumPath` path to chromium-executable (defaults to "/usr/bin/chromium-browser")
* `timeout` timeout in ms for waiting until the page has no more pending requests. resembles to puppeteer.goto() (defaults to 10000 --> 10s)
* `renderTimeout` timeout in ms for waiting AFTER the page has loaded before taking the screenshot. userful for PWAs. (defaults to 1ms)


# Usage
Expand Down
12 changes: 9 additions & 3 deletions ScreenshotHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ function ScreenshotHelper(log, url, chromiumPath = "/usr/bin/chromium-browser")
this.log("Initialized ScreenshotHelper");
}

ScreenshotHelper.prototype.getScreenshot = async function (width, height, timeout) {
ScreenshotHelper.prototype.sleep = function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
};

ScreenshotHelper.prototype.getScreenshot = async function (width, height, networkTimeout, renderTimeout) {
if (!this.browser) {
this.log("Starting new instance of Chromium: " + this.chromiumPath);
this.browser = await puppeteer.launch(
Expand All @@ -26,9 +30,11 @@ ScreenshotHelper.prototype.getScreenshot = async function (width, height, timeou
this.log("Setting Viewport to " + width + "x" + height);
await page.setViewport({width: width, height: height});
this.log("Going to page: " + this.url);
await page.goto(this.url, {waitUntil: 'networkidle2', timeout: timeout});
await page.goto(this.url, {waitUntil: 'networkidle2', timeout: networkTimeout});
this.log("Loading finished, waiting " + renderTimeout + "ms before taking screenshot");
await this.sleep(renderTimeout);
const screenshot = await page.screenshot({type: "jpeg"});
this.log("Created screenshot");
page.close();
return screenshot;
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-website-to-camera",
"version": "1.3.0",
"version": "1.4.0",
"description": "shows the screenshot of a website as camera (image)",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 625720e

Please sign in to comment.