Why will jQuery not load in Facebook? Why will jQuery not load in Facebook? jquery jquery

Why will jQuery not load in Facebook?


The Chrome console does not appear to have access to the content script's execution context.

Wrong, it does. You need to look at the correct place:

Animation of how-to get access to the execution environment of the Chromne extension

The previous screencast shows that the Console tab of the Chrome developer tools has two dropdown boxes at the bottom, which can be used to change the execution environment for the developer tools' console.
The left side can be used to change the frame context (top frame, so iframe, ...), and the right side can be used to change the script context (page, content script, ...).


The Answer

It seems my 'experimental method' was flawed. The assumption about the Chrome console's omniscience is incorrect. The Chrome console does not appear to have access to the content script's execution context. So although console was reporting that jQuery did not have access to the page, it actually did, from the content script's execution context.

This was verified by adding a content script, test.js, to manifest.json :

"content_scripts"         :   [                                  {                                    "matches"   : ["<all_urls>"],                                    "js"        : [                                                    "javascript/jq/jquery-1.9.1.min.js",                                                    "javascript/jq/non-standard.js",                                                    "javascript/test.js" // <-- add                                                  ],

The content of test.js is :

    var jtest = $('body');    alert(jtest);    alert(jtest.text());

Now whatever page I navigate to, the two alert boxes pop up as expected.

It works!


You may know all of these by now, but I think someone still finds these useful.

In a Chrome extension,

You have some "worlds of scripts":

  • Original page scripts: the scripts on the page itself.
  • Content scripts: you write those script, and they run on the page
  • Popup scripts: they run on the popup, if you have a popup page.
  • Background scripts: run on the global background page.

Google does a excellent job on documentation, so tons of document about all of those scripts https://developer.chrome.com/extensions.

But in your case, just note that: page scripts and content script live in separate worlds, they do share the DOM and some Chrome native objects, but they don't share variables (or objects) they create.

For this case, if you have jQuery in your content scripts, you will have $ and jQuery ready to use in your content scripts. You can use it to query the DOM (although some jQuery events may not work as expected). But on the page, you will not have $ and jQuery, (you might have $, but it is not jQuery ^^).

Your method 2 above, it actually injects jQuery into the page, that jQuery will become page script. And you cannot use $ or jQuery in your content scripts.

If you use both method at once, you can have jQuery 1 in your page scripts, and jQuery 2 in your content script, and they are 2 different jQuery instances. It might cause confusion, but I do it all the times.