0

When I try to include the jQuery library in my extension, the JS engine gives me an error. This is the code:

chrome.tabs.executeScript(null,{code:"
    if (typeof jQuery == 'undefined') { 
        var script = document.createElement('script');
        script.setAttribute('type') = 'text/javascript';
        script.setAttribute('src') = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'; 
        document.getElementsByTagName('head')[0].appendChild(script);
    };
"});

and this the result:

Uncaught ReferenceError: Invalid left-hand side in assignment (anonymous function)

This piece of code is written in the popup.js file, included by popup.html.

Even when i do something like this:

chrome.tabs.executeScript(null, {file: "browser.js", allFrames: true}, window.close());

and, in browser.js I specify the code written above (in which I include the jQuery library), it happens the same stuff. The strangest thing is that jQuery appears not to be defined at all, even when the tab in which the code is executed has already decleared it (such as Flickr, for example). In fact, typeof jQuery is always set to 'undefined'. I'm quite confused...

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
Gian Segato
  • 2,359
  • 3
  • 29
  • 37

4 Answers4

1
script.setAttribute('type') = 'text/javascript';

You are trying to assign a string to a method. Your syntax is invalid. The error has nothing to do with JQuery.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
1

You are using setAttribute incorrectly. You need to pass the value in as the second parameter rather than with assigning with an =

For example,

script.setAttribute('type', 'text/javascript');
Dan Smith
  • 5,685
  • 32
  • 33
  • Thank you very much! Even if jQuery is now successfully included, (typeof jQuery == 'undefined') still remain true, and I'm not able to understand the reason. – Gian Segato Jul 27 '12 at 14:58
  • @GianlucaSegato you don't need to set the script type at all (default is JS) and don't use `setAttribute()`, set the property directly: `script.src = '...'` – Matt Ball Jul 27 '12 at 15:07
0
script.setAttribute('type') = 'text/javascript';

That is invalid. You want:

script.setAttribute('type', 'text/javascript');

Same goes for the src attribute.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
0

Because it's syntactically invalid JavaScript, exactly as the error message tells you. The value returned by a function call cannot be the left-hand side of an assigment.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Yup, I was doing this in a very first moment. But I had some crazy errors like "jQuery is not defined", so I decided to pass through classic JS sintax - and in fact those errors never appeared again. In fact, I'm not able to include jQuery in my extension at all! – Gian Segato Jul 27 '12 at 16:39
  • http://stackoverflow.com/q/4947510/139010 or http://stackoverflow.com/q/8745463/139010 and [many more](http://stackoverflow.com/search?q=chrome+extension+jquery) – Matt Ball Jul 27 '12 at 16:48
  • I apologize for having bothered you for such a stupid issue. I was only not aware of the existence of "content_scripts" parameter. Thank you very much, you were very very helpful. – Gian Segato Jul 27 '12 at 20:03