Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting JQuery into Amazon.com pages using PhantomJS causes problems with JQuery

I'm opening any page in the Amazon.com domain (for example "http://www.amazon.com") and then attempting to inject JQuery like this:

var injected = page.injectJs('jquery-1.7.1.js');
console.log("jquery was injected successfully: " + injected);

This code will print true to the console. However, trying to access $(document) from within page.evaluate() like this:

page.onLoadFinished = function (status) {
    var results = page.evaluate(function() {
        $(document);    
    });

phantom.exit();
};

Will print TypeError: 'undefined' is not a function to the console.

This code works with most other domains I've tried with. I noticed that Amazon seems to have its own version of JQuery that it loads and I wonder if it might be conflicting with the JQuery version I'm loading somehow. Any ideas?

like image 259
grumblebeans Avatar asked Oct 23 '22 02:10

grumblebeans


1 Answers

You just need to put jQuery in noConflict mode with an alternate variable name like so:

var results = page.evaluate(function () {
    $jq = window.jQuery;
    $jq.noConflict();

    console.log("Title: " + $jq('title').text());
});

Worked for me! Hope it works for you! :)

like image 69
eComEvo Avatar answered Nov 15 '22 13:11

eComEvo