Skip to content

Commit

Permalink
Manual stop capture (#62)
Browse files Browse the repository at this point in the history
* Stop capture (#56)

* adds a `config.stopFunctionName` and `--stop-function-name` which allows the user to specify a function that can be called to stop capture

* updated cli.js and README.md for stopFunctionName

Co-authored-by: RhinoW <[email protected]>
Co-authored-by: user <[email protected]>
  • Loading branch information
3 people authored Jun 14, 2021
1 parent c37b034 commit b0fe2e8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ Opens https://breathejs.org/examples/Drawing-US-Counties.html, sets the viewport
* Suppresses console logging.
* <a name="cli-options-output-stdout" href="#cli-options-output-stdout">#</a> Output stdout: `--output-stdout`
* Outputs images to stdout. Useful for piping.
* <a name="cli-options-stop-function-name" href="#cli-options-stop-function-name">#</a> Stop Function Name: `--stop-function-name` *function name*
* Creates a function with *function name* that the client web page can call to stop capturing. For instance, `--stop-function-name=stopCapture` could be called in the client, via `stopCapture()`.
* <a name="cli-options-version" href="#cli-options-version">#</a> Version: `-v`, `--version`
* Displays version information. Immediately exits.
* <a name="cli-options-help" href="#cli-options-help">#</a> Help: `-h`, `--help`
Expand Down Expand Up @@ -282,6 +284,7 @@ The Node API is structured similarly to the command line options, but there are
* <a name="js-config-should-skip-frame-frame-count" href="#js-config-should-skip-frame-frame-count">#</a> `frameCount` &lt;[number][]&gt; The current frame count, starting at 1.
* <a name="js-config-should-skip-frame-frames-to-capture" href="#js-config-should-skip-frame-frames-to-capture">#</a> `framesToCapture` &lt;[number][]&gt; The total number of frames to be captured.
* <a name="js-config-should-skip-frame-page" href="#js-config-should-skip-frame-page">#</a> `page` &lt;[Page][]&gt; the puppeteer page.
* <a name="js-config-stop-function-name" href="#js-config-stop-function-name">#</a> `stopFunctionName` &lt;[string][]&gt; *function name* that the client web page can call to stop capturing. For instance, `'stopCapture'` could be called in the client, via `stopCapture()`.
* <a name="js-config-frame-processor" href="#js-config-frame-processor">#</a> `frameProcessor` &lt;[function][]([Buffer][], [number][], [number][])&gt; A function that will be called after capturing each frame. If `config.outputDirectory` and `config.outputPattern` aren't specified, enabling this suppresses automatic file output. After capturing each frame, `config.frameProcessor` is called with three arguments, and if it returns a promise, capture will be paused until the promise resolves:
* `screenshotData` &lt;[Buffer][]&gt; A buffer of the screenshot data.
* `frameNumber` &lt;[number][]&gt; The current frame number (1 based).
Expand Down
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ commander
// TODO: make a more sophisticated parser for options that can handle quote marks
return str.split(' ');
})
.option('--stop-function-name <function name>', 'Allows client page to call function name to stop capture')
.option('--no-headless', 'Chromium/Chrome runs in a window instead of headless mode')
.option('--screenshot-type <type>', 'Output image format for screenshots, either png or jpeg')
.option('--screenshot-quality <level>', 'The quality level for lossy screenshots', parseFloat)
Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = function (config) {
var unrandom = config.unrandomize;
var fps = config.fps, frameDuration;
var framesToCapture;
var requestStopCapture = false;
var outputPath = path.resolve(process.cwd(), (config.outputDirectory || './'));

if (!url.includes('://')) {
Expand Down Expand Up @@ -168,6 +169,11 @@ module.exports = function (config) {
return overwriteRandom(page, unrandom, log);
}).then(function () {
return timeHandler.overwriteTime(page);
}).then(function () {
// page can call the function with config.stopFunctionName to stop capture before --duration is up
if (config.stopFunctionName) {
return page.exposeFunction(config.stopFunctionName, () => requestStopCapture = true);
}
}).then(function () {
if (typeof config.navigatePageToURL === 'function') {
return config.navigatePageToURL({ page, url, log });
Expand Down Expand Up @@ -248,7 +254,7 @@ module.exports = function (config) {
var startCaptureTime = new Date().getTime();
var markerIndex = 0;
return promiseLoop(function () {
return markerIndex < markers.length;
return markerIndex < markers.length && !requestStopCapture;
}, function () {
var marker = markers[markerIndex];
var p;
Expand Down

0 comments on commit b0fe2e8

Please sign in to comment.