From b00c2e6b621472ae80740a938c8866ee481501b4 Mon Sep 17 00:00:00 2001 From: Evgeny Kruglov Date: Thu, 29 Sep 2016 21:56:16 +0200 Subject: [PATCH 1/3] Added onInitalized behaviour and fixed unmet dev dependency --- bridge.js | 17 ++++++++++++----- node-phantom-simple.js | 8 ++++++-- package.json | 3 ++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/bridge.js b/bridge.js index 3af46e2..43a8a50 100644 --- a/bridge.js +++ b/bridge.js @@ -147,7 +147,7 @@ var callbacks = [ 'onAuthPrompt' ]; -function setup_callbacks(id, page) { +function setup_callbacks(id, page, initCfg) { callbacks.forEach(function (cb) { page[cb] = function (parm) { var args = Array.prototype.slice.call(arguments); @@ -165,9 +165,16 @@ function setup_callbacks(id, page) { var new_id = setup_page(page); callback_stack.push({ page_id: id, callback: 'onPageCreated', args: [ new_id ] }); }; + + if (initCfg && initCfg.onInitialized){ + page.onInitialized = function (){ + // eslint-disable-next-line no-new-func + (Function('return ' + initCfg.onInitialized)())(page); + }; + } } -function setup_page(page) { +function setup_page(page, initCfg) { var id = page_id++; page.getProperty = function (prop) { return lookup(page, prop); @@ -199,7 +206,7 @@ function setup_page(page) { return true; }; pages[id] = page; - setup_callbacks(id, page); + setup_callbacks(id, page, initCfg); return id; } @@ -207,9 +214,9 @@ var global_methods = { setProxy: function (ip, port, proxyType, user, password) { return phantom.setProxy(ip, port, proxyType, user, password); }, - createPage: function () { + createPage: function (initCfg) { var page = webpage.create(); - var id = setup_page(page); + var id = setup_page(page, initCfg); return { page_id: id }; }, diff --git a/node-phantom-simple.js b/node-phantom-simple.js index ecaf33f..a640f12 100644 --- a/node-phantom-simple.js +++ b/node-phantom-simple.js @@ -522,8 +522,12 @@ exports.create = function (options, callback) { request_queue.push([ [ 0, 'setProxy', ip, port, proxyType, user, password ], callbackOrDummy(callback, poll_func) ]); }, - createPage: function (callback) { - request_queue.push([ [ 0, 'createPage' ], callbackOrDummy(callback, poll_func) ]); + createPage: function (callback, initCfg) { + if (initCfg && initCfg.onInitialized){ + initCfg.onInitialized = initCfg.onInitialized.toString(); + } + + request_queue.push([ [ 0, 'createPage', initCfg ], callbackOrDummy(callback, poll_func) ]); }, injectJs: function (filename, callback) { diff --git a/package.json b/package.json index 4498308..16835c5 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "devDependencies": { "eslint": "^3.4.0", "mocha": "^3.0.2", - "phantomjs": "^1.9.19" + "phantomjs": "^1.9.19", + "slimerjs": "^0.906.2" }, "dependencies": { "debug": "^2.2.0" From 2298bc9cac8d3c398964731fd721613c4f449807 Mon Sep 17 00:00:00 2001 From: Evgeny Kruglov Date: Sat, 1 Oct 2016 23:51:22 +0200 Subject: [PATCH 2/3] Added external function arguments --- bridge.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridge.js b/bridge.js index 43a8a50..4d09cb0 100644 --- a/bridge.js +++ b/bridge.js @@ -169,7 +169,7 @@ function setup_callbacks(id, page, initCfg) { if (initCfg && initCfg.onInitialized){ page.onInitialized = function (){ // eslint-disable-next-line no-new-func - (Function('return ' + initCfg.onInitialized)())(page); + (Function('return ' + initCfg.onInitialized)())(page,initCfg.onInitializedArgs); }; } } From db7c7c998e6ca2cb89ef469a2d8dcc7ec7573a32 Mon Sep 17 00:00:00 2001 From: Evgeny Kruglov Date: Sat, 1 Oct 2016 23:54:46 +0200 Subject: [PATCH 3/3] Fixed eslint error --- bridge.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridge.js b/bridge.js index 4d09cb0..a817a9a 100644 --- a/bridge.js +++ b/bridge.js @@ -169,7 +169,7 @@ function setup_callbacks(id, page, initCfg) { if (initCfg && initCfg.onInitialized){ page.onInitialized = function (){ // eslint-disable-next-line no-new-func - (Function('return ' + initCfg.onInitialized)())(page,initCfg.onInitializedArgs); + (Function('return ' + initCfg.onInitialized)())(page, initCfg.onInitializedArgs); }; } }