deanmao / node-chimera

A new kind of headless webkit integration for nodejs; a great alternative to phantomjs.
http://www.deanmao.com/2012/08/13/enter-chimera/
MIT License
847 stars 45 forks source link

Is run function executed every time when there is an ajax call or iframe after the page is loaded? #6

Closed shebinleo closed 11 years ago

shebinleo commented 11 years ago

Is there any way to execute the run function only once after the page is loaded? I have a page where immediately after the page is loaded an ajax call is made and have some iframes. The run function is executed more than once and it will reset the window variables to initial value.

deanmao commented 11 years ago

The code in run is run on every page -- so it's possible that there are several iframes on the page. This happens fairly often for pages with ads. The ads run inside an iframe, then the run() code is passed into those iframes. The ads will then refresh and the run() code will be executed again. You can put some js code in there to check what url the page is on and prevent it from running if it's not your target page.

shebinleo commented 11 years ago

Currently my run() code is executed twice and both shows the same url. Is there any way I can set a global variable and detect whether the run() code is executed?

deanmao commented 11 years ago

what's the url that is having this issue?

shebinleo commented 11 years ago

After I login to linkedin, the home page (http://www.linkedin.com/home) and the profile pages have this issue.

deanmao commented 11 years ago

I published a new version of chimera, and I also included an example linkedin script (inside the examples folder) that seems to work fine. Let me know if you have issues with it.

deanmao commented 11 years ago

closing this bug for now, if you reproduce it, please post a gist of your code and the url that you hit.

shebinleo commented 11 years ago

I am using the latest chimera now. My node application crashed with following error while using chimera.

Using the chimera in lib
Segmentation fault: 11

I am running it on Mac OS X 10.8.2.

deanmao commented 11 years ago

hm, it works for me. can you run node through gdb and provide a stacktrace?

instructions:

gdb node
run example.js
bt

I'm also on Mac OS X 10.8.2, node v0.8.16.

What node version are you running?

deanmao commented 11 years ago

I published a new package that might prevent the segfault. I'm not sure why it happens for you, but I can sometimes recreate the segfault. I introduced a hack that might prevent it for you. This is node-chimera version 0.3.1 (published just now).

Let me know if you still get the segmentation fault. If you do, provide me with your node version and a gdb backtrace.

shebinleo commented 11 years ago

Thank you for the quick update. The segmentation fault is fixed.

Regarding the original behavior I reported, I was able to reproduce this on your example linkedin account. I followed a few companies to populate the home page. Attached below is the updated linkedin login code. The output I get is as follows.

webkit -- our location: http://www.linkedin.com/
webkit -- found login form
webkit -- 930-49
webkit -- our location: http://www.linkedin.com/home?trk=guest_home
webkit -- we are logged in!
webkit -- our location: http://www.linkedin.com/home?trk=guest_home
webkit -- we are logged in!

But if I change/remove the disableImages to false, the logged in logic is executed only once.

By the way, thank you for this module. It is really helpful.

var c = new Chimera({disableImages: true, libraryCode: '(function() {' + jquery+ '; window.myjquery = jQuery; jQuery.noConflict(true);})()'});
c.perform({
  url: "http://www.linkedin.com",
  locals: {
    username: myUsername,
    password: myPassword
  },
  run: function(callback) {
    try {
      console.log('our location: '+window.location.toString());
      var loginForm = myjquery('form[name=login]');
      if (loginForm[0]) {
        console.log('found login form');
        loginForm.find('input[name=session_key]').val(username);
        loginForm.find('input[name=session_password]').val(password);
        var pos = loginForm.find('#btn-login').offset();
        console.log(pos.left + '-' + pos.top);
        chimera.capture("logged_out.png");
        chimera.sendEvent("click", pos.left + 5, pos.top + 5);
      } else if (myjquery('a.username')[0]) {
        console.log('we are logged in!');
        chimera.capture("logged_in.png");
      }
    } catch (e) {
      console.log('error in the chimera script');
      console.log(e);
    }
  },
  callback: function(err, result) {
    console.log('All scripts are done');
    c.close();
  }
});
deanmao commented 11 years ago

That's strange, it only happens on your system.... maybe linkedin is doing some A/B testing and you're getting the page with a hidden iframe somewhere. After logging in, I don't get redirected to the same url as you. I just get http://www.linkedin.com/home. I don't get that guest_home url that you have.