From 12a7813aabf34131d426046b0d0a0a341b03e385 Mon Sep 17 00:00:00 2001 From: Antonin Stefanutti Date: Mon, 7 Sep 2015 21:40:41 +0200 Subject: [PATCH] Add maxSlides option for the generic plugin --- README.md | 6 +++++- plugins/generic.js | 15 +++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f6cdd47..bd26aa1 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,11 @@ Iterates over the [available plugins](/plugins), picks the compatible one for pr ### `generic` -Emulates the end-user interaction by pressing the key with the specified `keycode` and iterates over the presentation as long as any change to the DOM is detected by observing mutation events to the body element and its subtree. The `keycode` value must be one of the [PhantomJS page event keys](https://github.com/ariya/phantomjs/blob/cab2635e66d74b7e665c44400b8b20a8f225153a/src/modules/webpage.js#L329) and defaults to `Right`, e.g.: +Emulates the end-user interaction by pressing the key with the specified `keycode` option and iterates over the presentation as long as: ++ Any change to the DOM is detected by observing mutation events targeting the body element and its subtree, ++ Nor the number of slides exported has reached the specified `maxSlides` option. + +The `keycode` value must be one of the [PhantomJS page event keys](https://github.com/ariya/phantomjs/blob/cab2635e66d74b7e665c44400b8b20a8f225153a/src/modules/webpage.js#L329) and defaults to `Right`, e.g.: ``` phantomjs decktape.js generic --keycode=Space diff --git a/plugins/generic.js b/plugins/generic.js index 1abc6b9..7730e86 100644 --- a/plugins/generic.js +++ b/plugins/generic.js @@ -5,14 +5,19 @@ exports.options = { keycode: { default: "Right", help: "Key code pressed to navigate to next slide" + }, + maxSlides: { + help: "Maximum number of slides to export" } }; exports.help = - "Emulates the end-user interaction by pressing the key with the specified [keycode]\n" + - "and iterates over the presentation as long as any change to the DOM is detected\n" + - "by observing mutation events to the body element and its subtree.\n" + - "The [keycode] must be one of the PhantomJS page event keys and defaults to [Right]."; + "Emulates the end-user interaction by pressing the key with the specified [keycode] option\n" + + "and iterates over the presentation as long as:\n" + + "- Any change to the DOM is detected by observing mutation events targeting the body element\n" + + " and its subtree,\n" + + "- Nor the number of slides exported has reached the specified [maxSlides] option.\n" + + "The [keycode] option must be one of the PhantomJS page event keys and defaults to [Right]."; exports.create = function (page, options) { return new Generic(page, options); @@ -55,6 +60,8 @@ Generic.prototype = { // A priori knowledge is impossible to achieve in a generic way. Thus the only way is to actually emulate end-user interaction by pressing the configured key and check whether the DOM has changed a posteriori. hasNextSlide: function () { + if (this.options.maxSlides && this.currentSlide >= this.options.maxSlides) + return false; // PhantomJS actually sends a 'keydown' DOM event when sending a 'keypress' user event. Hence 'keypress' event is skipped to avoid moving forward two steps instead of one. See https://github.com/ariya/phantomjs/issues/11094 for more details. ["keydown"/*, "keypress"*/, "keyup"].forEach(function (event) { this.page.sendEvent(event, this.keycode);