4

I am using Google Chrome and I'm writing a browser extension. I'm trying to inject JQuery 2.0.3 into facebook as a context script. Just to see if I could, I'm trying to grab the text area on the message page. I'm typing this line directly into the Javascript console:

$('textarea[name="message_body"]');

Instead of getting a JQuery object I'm getting this error:

Error: <![EX[["Tried to get element with id of \"%s\" but it is not present on the page.","undefined"]]]>

Does facebook have anti-jquery measures or is there something else happening that I'm doing wrong? As usual, thanks all in advance!

Niko
  • 4,158
  • 9
  • 46
  • 85
  • Are you sure the above error is a result of this particular line of code ? It might be related to some other part of the code, so posting a little more "context" would be helpful. – gkalpak Nov 29 '13 at 07:18
  • 1
    Yes, this line of code is being typed directly in the javascript console. This is the only line unfortunately, so there is not more context for me to give =\ (I'm adding that detail to the question for future clarity) – Niko Nov 29 '13 at 07:53
  • The content scripts "live" in an isolated world. Thus the JS context of the web-page (which can be acdessed from the console) knows nothing about the JS context of the content script and vice versa. So the console knows nothing about your injecting `jQuery 2.0.3`. In order for it to work yoh should execute this line if code from a content script. – gkalpak Nov 29 '13 at 09:00
  • While they are isolated, I know from personal experience that you can still access the content scripts from the console. It may just be the difference between a packed and unpacked extension (although I haven't tested if that's what makes a difference, but the extension is currently unpacked). Additionally I do know that it does work as I've been able to use JQuery in the console on other pages that do not ship with JQuery. Test it yourself if you don't believe me. – Niko Nov 29 '13 at 09:55
  • Unless you inject jQuery into the web-page's DOM (e.g. inserting a ` – gkalpak Nov 29 '13 at 10:41
  • To access the execution context of the content scripts from the console, you need to select it from the drop down box located at the bottom of the window. – rsanchez Nov 29 '13 at 11:45
  • Yes, but the two JS contexts are separate. Thus you cannot access injected jQuery from the web-pages context. – gkalpak Nov 29 '13 at 12:54
  • possible duplicate of [Why will jQuery not load in Facebook?](http://stackoverflow.com/questions/15194699/why-will-jquery-not-load-in-facebook) – Rob W Nov 29 '13 at 19:08

1 Answers1

17

You are accessing a $() function defined in the Facebook page, which has nothing to do with jQuery. To access the execution context of you content scripts from the javascript console, you need to select it from the drop down box located at the bottom of the window:

Bottom bar of Developer Tools

Instead of <page context>, you have to select chrome-extension://<your extension id>.

rsanchez
  • 14,467
  • 1
  • 35
  • 46
  • Interesting, I wonder how I was doing it on the other pages. I may have had it selected already and not known it. Either way this was exactly what I needed. Thank you! – Niko Nov 30 '13 at 01:38
  • I bet you have a good answer to this question @rsanchez https://stackoverflow.com/questions/53346451/chrome-extension-not-loading-code-environment-not-selectable-in-inspector-dev/57163028#57163028 – Craig Lambie Jul 23 '19 at 12:00