Skip to content
This repository has been archived by the owner on Dec 4, 2019. It is now read-only.

Commit

Permalink
With this commit we can pass extra arguments to onResourceRequested f…
Browse files Browse the repository at this point in the history
…unction which runs in phantomjs scope. (doesn't support function, also no need to do that)
  • Loading branch information
Fan Wenbo committed Dec 24, 2014
1 parent 47334bf commit e0ce46b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
7 changes: 4 additions & 3 deletions README.markdown
Expand Up @@ -99,11 +99,12 @@ Due to the async nature of the bridge, some things have changed, though:
* ```page.render()``` takes a callback so you can tell when it's done writing the file
* Properties can't be get/set directly, instead use ```page.get('version', callback)``` or ```page.set('viewportSize', {width:640,height:480})```, etc. Nested objects can be accessed by including dots in keys, such as ```page.set('settings.loadImages', false)```
* Callbacks can't be set directly, instead use ```page.set('callbackName', callback)```, e.g. ```page.set('onLoadFinished', function(success) {})```
* onResourceRequested takes a function that executes in the scope of phantom which has access to ```request.abort()```, ```request.changeUrl(url)```, and ```request.setHeader(key,value)```. The second argument is the callback which can execute in the scope of your code, with access to just the requestData. e.g.
* onResourceRequested takes a function that executes in the scope of phantom which has access to ```request.abort()```, ```request.changeUrl(url)```, and ```request.setHeader(key,value)```. The second argument is the callback which can execute in the scope of your code, with access to just the requestData. This function can apply extra arguments which can be passed into the first function e.g.
```
page.onResourceRequested(
function(requestData, request) { request.abort(); },
function(requestData) { console.log(requestData.url) }
function(requestData, request, arg1, arg2) { request.abort(); },
function(requestData) { console.log(requestData.url) },
arg1, arg2
);
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -3,7 +3,7 @@
"name": "phantom",
"description": "PhantomJS wrapper for Node",
"homepage": "https://github.com/sgentle/phantomjs-node",
"version": "0.7.1",
"version": "0.7.2",
"repository": {
"type": "git",
"url": "git://github.com/sgentle/phantomjs-node.git"
Expand Down
3 changes: 2 additions & 1 deletion phantom.coffee
Expand Up @@ -33,7 +33,8 @@ wrap = (ph) ->
page._evaluate = page.evaluate
page.evaluate = (fn, cb, args...) -> page._evaluate.apply(page, [fn.toString(), cb].concat(args))
page._onResourceRequested = page.onResourceRequested
page.onResourceRequested = (fn, cb) -> page._onResourceRequested.apply(page, [fn.toString(), cb])
# can apply extra args which will be passed to phantomjs onResourceRequested scope
page.onResourceRequested = (fn, cb, args...) -> page._onResourceRequested.apply(page, [fn.toString(), cb].concat(args))
cb page

module.exports =
Expand Down
7 changes: 5 additions & 2 deletions phantom.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions shim.coffee
Expand Up @@ -75,16 +75,18 @@ pageWrap = (page) -> mkwrap page,
page.onConsoleMessage = ->
fn.apply(this, arguments)
cb()
onResourceRequested: (fn, cb=(->)) ->
onResourceRequested: (fn, cb=(->), args...) ->
page.onResourceRequested = ->
# prepare a arguments with the extra args
argumentsWithExtraArgs = [].slice.apply(arguments).concat(args)
# give a name to the anonymouse function so that we can call it
fn = fn.replace /function.*\(/, 'function x('
# the only way we can access the request object is by passing a function to this point as a string and expanding it
eval(fn) # :(
# this function has access to request.abort()
x.apply(this, arguments)
x.apply(this, argumentsWithExtraArgs)
# this function does not have access to request.abort()
cb.apply(this, arguments)
cb.apply(this, argumentsWithExtraArgs)
injectJs: (js, cb=->) -> cb page.injectJs js
evaluate: (fn, cb=(->), args...) -> cb page.evaluate.apply(page, [fn].concat(args))
render: (file, opts={}, cb) ->
Expand Down
10 changes: 7 additions & 3 deletions shim.js
Expand Up @@ -5511,13 +5511,17 @@ require.define("/shim.coffee", function (require, module, exports, __dirname, __
};
return cb();
},
onResourceRequested: function(fn, cb) {
onResourceRequested: function() {
var args, cb, fn;
fn = arguments[0], cb = arguments[1], args = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
if (cb == null) cb = (function() {});
return page.onResourceRequested = function() {
var argumentsWithExtraArgs;
argumentsWithExtraArgs = [].slice.apply(arguments).concat(args);
fn = fn.replace(/function.*\(/, 'function x(');
eval(fn);
x.apply(this, arguments);
return cb.apply(this, arguments);
x.apply(this, argumentsWithExtraArgs);
return cb.apply(this, argumentsWithExtraArgs);
};
},
injectJs: function(js, cb) {
Expand Down

0 comments on commit e0ce46b

Please sign in to comment.