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

前端 未结 4 745
隐瞒了意图╮
隐瞒了意图╮ 2021-02-19 13:13

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

相关标签:
4条回答
  • 2021-02-19 13:50

    The ability to change the context/scope of the Web Console (opened directly with Ctrl-Shift-K or F12 and selecting the Console tab) to that of the content scripts for an extension does not appear to exist. In addition, this capability does not exist in any of the other ways to view a console in Firefox. A bug/RFE should be filed on Bugzilla requesting this functionality; it would be quite useful. You will want the RFE to clearly explain that there should be the ability to switch to the content script context/scope for each frame in the tab (i.e. the top frame and each child frame). This should be the case for both the Console and the Debugger.

    NOTE: 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:

    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 A) does not switch into the content script context/scope and B) does not work properly with the Web Debugger (testing in the current version of Firefox and Nightly (54.0a1).

    Web Debugger

    You can use the Web Debugger (Ctrl-Shift-S, or F12 and selecting the Debugger tab) with WebExtension content scripts. The content scripts for the extension are listed in the "Sources" under a moz-extension:// URL. You will need to identify the UUID that is used for the extension. 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 the child frame. Placing debugger; directives in the JavaScript which is running in the child iframe is ineffective.

    Web 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 clicking on the Debug button for your extension in about: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. To view these attributes, I find that the DOM Inspector shows these quite clearly:

      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.

    0 讨论(0)
  • 2021-02-19 14:10

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

    0 讨论(0)
  • 2021-02-19 14:11

    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:

    0 讨论(0)
  • 2021-02-19 14:11

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

    0 讨论(0)
提交回复
热议问题