From 46a0949386e6df76bf7ce979bb2030e77e9624ee Mon Sep 17 00:00:00 2001 From: Jesse Gavin Date: Tue, 4 Oct 2016 15:32:38 -0500 Subject: [PATCH 1/3] Added captureOnCallback option so that Manet can make use of page.onCallback() feature. --- public/js/app.js | 6 ++++-- public/usage.html | 5 +++++ src/options.js | 3 ++- src/scripts/screenshot.js | 39 ++++++++++++++++++++++++++++----------- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/public/js/app.js b/public/js/app.js index 64ba99f..6c1eef8 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -9,12 +9,14 @@ 'user', 'password', 'callback', 'headers', 'clipRect', 'force', 'selector', - 'engine' + 'engine', + 'captureOnCallback' ]; function cleanBoolValue(name, value) { return ((value && (name === 'js' || name === 'images')) || - (!value && name === 'force')) ? null : value; + (!value && name === 'force') || + (!value && name === 'captureOnCallback')) ? null : value; } function readOptions() { diff --git a/public/usage.html b/public/usage.html index d23357e..e5bd443 100644 --- a/public/usage.html +++ b/public/usage.html @@ -84,6 +84,11 @@

Website screenshot service

+
+ + +
+
diff --git a/src/options.js b/src/options.js index 65f317a..0e5c657 100644 --- a/src/options.js +++ b/src/options.js @@ -41,7 +41,8 @@ function createSchema() { secure: joi.boolean(), expires: joi.string() }) - ) + ), + captureOnCallback: joi.boolean() }); } diff --git a/src/scripts/screenshot.js b/src/scripts/screenshot.js index 91241c6..8d9e8ba 100644 --- a/src/scripts/screenshot.js +++ b/src/scripts/screenshot.js @@ -221,16 +221,33 @@ try { var page = createPage(options, captureScreenshot); + var addStylesAndRender = function() { + try { + addStyles(page, DEF_STYLES); + renderScreenshotFile(page, options); + } catch (e) { + exit(page, e); + } + } + + if (options.captureOnCallback === true) { + page.onCallback = function(data) { + log('CALLBACK: '+ JSON.stringify(data)); + addStylesAndRender(); + } + } + page.open(options.url, function (status) { - var onPageReady = function() { - try { - addStyles(page, DEF_STYLES); - renderScreenshotFile(page, options); - } catch (e) { - exit(page, e); - } - }, - checkDomElementAvailable = function() { + + if (status !== 'success') { + exit(); + } + + if (options.captureOnCallback === true) { + return; + } + + var checkDomElementAvailable = function() { if (options.selector) { var interval = setInterval(function () { var element = page.evaluate(function (selector) { @@ -238,12 +255,12 @@ }, options.selector); if (element !== null && typeof element === 'object') { - onPageReady(); + addStylesAndRender(); clearInterval(interval); } }, 250); } else { - onPageReady(); + addStylesAndRender(); } }, checkReadyState = function() { From 9d134e70f65b9755cfb71d1205f778f4feb3ac5c Mon Sep 17 00:00:00 2001 From: Jesse Gavin Date: Tue, 4 Oct 2016 15:46:37 -0500 Subject: [PATCH 2/3] Make jshint happy. --- src/scripts/screenshot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scripts/screenshot.js b/src/scripts/screenshot.js index 8d9e8ba..14bdb6c 100644 --- a/src/scripts/screenshot.js +++ b/src/scripts/screenshot.js @@ -228,13 +228,13 @@ } catch (e) { exit(page, e); } - } + }; if (options.captureOnCallback === true) { page.onCallback = function(data) { log('CALLBACK: '+ JSON.stringify(data)); addStylesAndRender(); - } + }; } page.open(options.url, function (status) { From c4fb3a63b20a33800ba1fe44e32d1f63d4d89ae8 Mon Sep 17 00:00:00 2001 From: Jesse Gavin Date: Tue, 4 Oct 2016 17:02:15 -0500 Subject: [PATCH 3/3] Added parameter to readme --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index aae26a6..ad873db 100644 --- a/README.md +++ b/README.md @@ -335,6 +335,9 @@ Few rules:
cookies
Configure cookies that will be contained in request. HTTP message body is the easiest way for sending cookies to Manet (ex: using JSON format).
+
captureOnCallback
+
Allow the page to tell Manet when it's ready for capture. This uses page.onCallback under the hood. (default is `false`)
+