How to access add-on content script in Firefox console?
A

4

18

I have developed an add-on for Firefox and Chrome. It has content scripts. I want to access them in the browser tab's console (on Firefox the Web Console). For example, I want to enter a global variable defined in the content script(s) in the console, and it will output its value.

In Chrome, I can open the console by pressing F12, then navigate to the Console tab in the developer tools. It has a dropbox, right after the filter button, to select which context I am in (page/content script):

In Firefox, how to do the same thing?

Adelladella answered 3/3, 2017 at 6:1 Comment(6)
Please define what you mean by "access to them" (e.g. do you mean view in debugger (just switch to the debugging tab)?, enter JavaScript into the Console in that context?, or something else?). Please keep in mind that what you are calling the browser console is actually the Web Console. The Browser Console (Ctrl-Shift-J, or Cmd-Shift-J on OSX) has a specific meaning for Firefox. There are various consoles in Firefox and Chrome, which each have their appropriate uses.Monetmoneta
@Monetmoneta By access, I mean for example, I can enter a global variable defined in content scripts in console, and it will output its value.Adelladella
Is viewing the variable's value in the debugger sufficient? You can do that by selecting the code file in the Debugger tab. To be more explicit: Is what you need something that can only be handled by being able to enter lines of code in a console and have them execute within the content script's context/scope?Monetmoneta
@Monetmoneta My extension doesn't work well for iframes. Firstly I want to make sure, if iframes have loaded my content scripts (by checking if global variables exist in content script console). View variables in debugger can be ambiguous, because I don't know where I am (Am I in page? Or am I in iframe?)Adelladella
I don't see a good solution for exactly what you want (you should submit a bug/RFE in bugzilla. I'm still not sure exactly what you're really trying to do, other then that it is differentiating values between the main frame/iframe. From your description, I might try setting info in the DOM using Element.dataset. These are then quite easily visible in the DOM Inspector.Monetmoneta
@Monetmoneta Your suggestion will do the trick. Thank you! :-)Adelladella
M
8

The ability to do this doesn't exist

The ability to change the context/scope of the Web Console to that of the an extension's content scripts does not exist. In addition, this capability does not exist in any of the other ways to view a console in Firefox. A bug/RFE is filed on Bugzilla requesting this functionality, with an overall tracking bug for the feature.

You can change the console's context/scope to an iframe's page scripts

You can change the console into the context/scope of the iframe's page scripts by selecting the frame from the drop-down menu opened from the frame-selector drop-down:

Screen capture of changing the console context/scope to an iframe

If this drop-down icon is not appearing for you, go to the DevTools settings and check "Select an iframe as the currently targeted document". However, doing this:

  • does not switch into the content script context/scope, and
  • may not work properly with the JavaScript Debugger, but this may have been improved since this answer was written in 2017.

JavaScript Debugger

You can use the JavaScript Debugger with WebExtensions content scripts. The content scripts for the extension are listed in the "Sources" section under the name of the extension, then the filename of the content script. You can view the content of variables, set breakpoints, etc. However, this does not give you the ability to explicitly switch to the context of a child frame. Placing debugger; directives in the JavaScript which is running in the child iframe's content script is ineffective at getting the console context to change to the content script context.

JavaScript Debugger debugging content script (in top frame) [This screenshot is quite old. While the current general layout is the same, there have been significant improvements.]:

Screenshot showing Firefox JavaScript Debugger debugging content script (in top frame)

Console in background script context

If you were wanting to open a console which was in the context of your WebExtensions' background script, you could do so by navigating to "about:debugging", clicking to use "This Firefox" for debugging extensions and service workers, then clicking on the "Inspect" button for the extension you are interested in debugging. However, this will not get you access to a console in the content script's context.

Workarounds for seeing variable values in the iframe

For what you need: unambiguously indicating that values are in the iframe context, not the top frame; I see two methods of doing so:

  • Use console.log() with information prepended that clearly indicates that the script believes it is running in the iframe. For example:

    console.log('In iframe', 'foo=', foo);
    

    So that you don't have to have 'In iframe' in every call to console.log() you make, you could create a function that prepends that text to all calls to that function. You could even override the console.log() function so your code still just calls console.log().

    However, this only tells you that your code thinks that it is running in the iframe. Part of what you may be debugging is your content script code detecting that it is in an iframe.

    This method does not indicate with certainty that the reported values are actually in the iframe.

  • Store values into the DOM using Element.dataset, or other DOM element attributes, and then inspect the DOM for these values. You can then use the Page Inspector to view these attributes.

    This method can be used to unambiguously show that the values are ones in the iframe, and exactly which iframe, without relying on the code running in the iframe to accurately determine that it is in an iframe and which iframe it is in.

Monetmoneta answered 3/3, 2017 at 17:40 Comment(6)
any solution for this craziness?Aguilar
Please clarify which iframe your answer refers to.Hurried
@PhilippeCloutier It refers to any iframe. While the question doesn't mention an iframe, each iframe is an additional set of contexts/scopes (e.g., the iframe's page scripts context, and a context for each extension's content scripts which are running in that iframe). Thus, while it wasn't contemplated by how the question was asked, it's something that needs to be addressed for the idea of having a console that is in the extension's content script context (e.g., there can be multiple such contexts for that extension's content scripts in the page and page's iframes).Monetmoneta
@Monetmoneta so if the question presumably doesn't involve any iframe, should answers not refrain from assuming that there will be an iframe?Hurried
@PhilippeCloutier Well, that depends. Do you want a comprehensive answer about what's possible in situations which the question is asking about "How to access add-on content script in Firefox console?", or do you want a narrow answer that looks at only the very limited expansion of that question which the OP thought to ask about the broader issue that they are actually struggling with? Note that my answer isn't assuming that there is an iframe. It just discusses the additional situation of there being an iframe, in addition to the case of there not being an iframe.Monetmoneta
@Makyen: We want at least one of the two. This answer clearly assumes that there is an iframe (and even that it is relevant to the solution); just search for the occurrences of "the iframe" if you need to be convinced.Hurried
H
2

A simple solution is to just console.log() in the content script and then click the sourcemap link to view the script. As shown below:

enter image description here

Hydrotherapy answered 16/3, 2020 at 23:29 Comment(1)
Well, I can confirm that it works in Firefox 86. Try console logging in the extension on page load, then it should come up in the dev tools. You won't be able to explore the file tree of the extension though...Hydrotherapy
S
2

It's not yet possible. There is a bug Implement UI for switching context to content script opened (since Nov 2017) for that.

Sinnard answered 19/11, 2020 at 2:49 Comment(0)
C
-1

In Firefox Developer Edition, go on "about:debugging" page and click on the "Debug" button beside your add-on to open the dev tools.

Centro answered 3/3, 2017 at 12:41 Comment(1)
That will open console for extension's background scripts, not content scripts.Adelladella

© 2022 - 2024 — McMap. All rights reserved.