Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to override the options during the takeShot callback #125

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
*.swp
node_modules
.ds_store
.ds_store

# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio
*.iml
.idea/
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,23 @@ var options = {
};
```

### Override options in takeShotOnCallback
When using the takeShotOnCallback option, the page can override the options by
providing an object instead of the string as follows:

```javascript
window.callPhantom({
event: 'takeShot',
options: {
shotSize: {
width: document.getElementsByTagName('html')[0].offsetWidth,
height: document.getElementsByTagName('html')[0].offsetHeight
}
}
});
```
In this example the shot size will be set to the real size of the document.

## Tests
Tests are written with [Mocha](http://visionmedia.github.com/mocha/) and can be
run with `npm test`. The tests use [node-imagemagick](http://github.com/rsms/node-imagemagick) and thus require
Expand Down
10 changes: 9 additions & 1 deletion lib/webshot.phantom.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@ optUtils.phantomPage.forEach(function(key) {
});

// The function that actually performs the screen rendering
var _takeScreenshot = function(status) {
var _takeScreenshot = function(status, optionsOverride) {
if (status === 'fail') {
page.close();
phantom.exit(1);
return;
}

if (typeof optionsOverride == 'object') {
var assign = require('object.assign').getPolyfill();
assign(options, optionsOverride);
}

// Wait `options.renderDelay` seconds for the page's JS to kick in
window.setTimeout(function () {

Expand Down Expand Up @@ -136,6 +141,9 @@ if (options.onCallback && options.takeShotOnCallback) {
if (data == 'takeShot') {
_takeScreenshot();
}
else if (data.event == 'takeShot') {
_takeScreenshot(null, data.options);
}
};
} else if (options.onLoadFinished && !options.takeShotOnCallback) {
takeScreenshot = function(status) {
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
{
"name": "Matthew Chase Whittemore",
"email": "[email protected]"
},
{
"name": "Sergey Fayngold",
"email": "[email protected]"
}
],
"license": "MIT",
Expand All @@ -22,8 +26,9 @@
"node >=0.7.00"
],
"dependencies": {
"graceful-fs": "~3.0.4",
"cross-spawn": "^0.2.3",
"graceful-fs": "~3.0.4",
"object.assign": "^4.0.3",
"tmp": "~0.0.25"
},
"optionalDependencies": {
Expand Down