CapMousse / include.js

A tiny but heavy on-demand async javascript/css loader
http://capmousse.github.io/include.js/
MIT License
146 stars 22 forks source link

compatability with phantomjs? #8

Closed nicferrier closed 12 years ago

nicferrier commented 12 years ago

I tried using this with phantomjs but didn't get anywhere. Is it possible that include does something that phantomjs doesnt' like?


try {
  var system = require('system');
  var page = require('webpage').create();
  page.onConsoleMessage = function(msg) {
    console.log("page: " + msg);
  };
  page.open(
    "http://localhost:8005/talk/stuff/html/index.html", 
    function (status) {        
      if (status == "success") {
        page.includeJs(
          "http://localhost:8005/talk/stuff/include.js/include.js", function () {
             page.evaluate(function () {
                include([
                  "/talk/stuff/js/client.js",
                ], function () {
                  console.log("all done including");
                });
            });
            console.log("all done");
            phantom.exit();
          });
      }
      else {
        console.log("an error loading the page");
        phantom.exit();
      }
    });
}
catch (e) {
  console.log(e);
  phantom.exit();
}

that's my code, though you obviously need to muck about with the urls to have someting that works.

I was using phantomjs 1.5.

CapMousse commented 12 years ago

This is a strange behavior.

Maybe PhantomJS use a internal "include" function that overwrite the Include.js method ?

http://localhost:8005/talk/stuff/include.js/include.js is a good address ?

I try your code and come back soon

CapMousse commented 12 years ago

Yep, look's like include() cannot be used on a phantomJS script !

I need to 'fix' include to the window element and not to the global scope (overwriting a PhantomJS function).

nicferrier commented 12 years ago

That would be very very awesome.

CapMousse commented 12 years ago

Ok.

I finally manage to make Include Working on PhantomJS. Look's like PhantomJS don't like the async way of Include and don't wait Include to finish is task before exit.

So, the trick is to don't send a final phantom.exit() :

try {
    var system = require('system'),
        page = require('webpage').create();

    page.onConsoleMessage = function (msg) { console.log(msg); };

    page.open("http://localhost/", function(status){
        console.log('page loaded');

        page.includeJs('http://capmousse.github.com/include.js/tests/scripts/include-min.js', function(){
            page.evaluate(function(){
                try {
                    include(['https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'], function(){
                        console.log($); // here prompt function(a, b... Working as expected
                    });
                    console.log('Include JS');
                }catch(e){
                    console.log(e);
                    phantom.exit();
                }
            });
        });
    });
}catch(e){
    console.log(e);
    phantom.exit();
}

I'm not a PhantomJS badass. I think there is a way to wait for a script to be done before exiting

CapMousse commented 12 years ago

Any news ?

nicferrier commented 12 years ago

Sorry, I will try and give it a try... I switched to another includer for now but I will come back to include to try it. Thanks for making the patch!