Kim Strauss
Kim Strauss

Reputation: 329

JavaScript objects visible in FireBug, inaccessible in code

In my code I have a line that dumps the current window (which happens to be a youtube video page):

Firebug.Console.log(myWindow);

It can be seen that window object contains "yt" property, which is another object that can be easily inspected in debugger:

https://i.sstatic.net/AA4Z6.png

Unfortunately, calling

 Firebug.Console.log(myWindow.yt);

logs "undefined" - why is that, and how can I access this "yt" property?

Edit: one addidtion that might be important: the code I'm writing is part of a firefox extension, so it's not really running inside a pgae, but in chrome - I'm starting to think that it may be the cause. Can chrome scripts be somehow limited in what they can see/acces as opposed to code in script tags?

Upvotes: 5

Views: 283

Answers (3)

WizardsOfWor
WizardsOfWor

Reputation: 3144

I have seen confusion like this using asynchronous APIs.

console.log(obj); shows the contents of an object all filled in, but when accessing the object properties in code, they aren't really populated yet due to the call being asynchronous.

Why Chrome and Firefox shows them all filled in is probably just a timing issue as they probably process the console.log() asynchronously as well.

Upvotes: 0

Wladimir Palant
Wladimir Palant

Reputation: 57681

For security reasons, Firefox extensions don't access web page objects directly but via a wrapper. This wrapper allows you to use all properties defined by the DOM objects but anything added by page JavaScript will be invisible. You can access the original object:

Firebug.Console.log(XPCNativeWrapper.wrappedJSObject.yt);

However, if you want to interact with the web page from an extension you should consider alternatives where the web page cannot play tricks on you (e.g. running unprivileged code in the content window: myWindow.location.href = "javascript:...").

Upvotes: 2

Kevin Ennis
Kevin Ennis

Reputation: 14464

Firefox and Chrome extensions can't access JavaScript within the page for security reasons.

Upvotes: 0

Related Questions