-
Notifications
You must be signed in to change notification settings - Fork 44
Adding support for calling back from phantom to node #55
base: master
Are you sure you want to change the base?
Conversation
Thank you for your pull-request. Can you describe a use-case where this |
Hi jhnns I've added this functionally because I needed to initiate an action base options for events triggered at the phantomjs client side code. Thanks |
You're right. There is currently no way to achieve this. However, there are some problems with your implementation. What happens if I have multiple instances of For instance, we could have a third argument phantom.run(function (resolve, reject, emit) {
setInterval(() => {
emit("hi");
}, 100);
resolve();
});
somePage.run(function (resolve, reject, emit) {
setInterval(() => {
emit("hi");
}, 100);
});
phantom.on("hi", function () { ... });
somePage.on("hi", function () { ... }); This way...
However, there might be another problem: When we're inside PhantomJS, we are not inside the actual page context. When using the page.run(function (resolve, reject, emit) {
this.evaluate(function () {
// emit is not defined because we're in the actual page context now
emit("hi");
});
resolve();
}); We would need to use the page.run(function (resolve, reject, emit) {
this.onCallback(function (message) {
emit(message);
});
this.evaluate(function () {
window.callPhantom('hi');
});
resolve();
}); This is very ugly – and again, we have the same problem that we need to collate callbacks from different sources... I totally see the use-case and I wish, phridge would provide an easy API for this. But a proper implementation takes a lot of effort... For instance, it would be cool to have a page.evaluate(function (resolve, reject, emit) {
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
emit("mutation", mutation.type);
});
});
observer.observe(document.body, {
attributes: true,
childList: true,
characterData: true
});
});
page.on("mutation", function (mutationType) {
}); |
- Fixes unexpectedExit handling - Closing mechanism now works well
I want to be able to call from the phantom code to the node module.
The code added a function calls "nodeCallback" at the phantom side and property calls "onCallback" on the "page" object.