简体   繁体   English

如何在 Firefox 控制台中访问附加内容脚本?

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

I have developed an add-on for Firefox and Chrome.我为 Firefox 和 Chrome 开发了一个附加组件。 It has content scripts.它有内容脚本。 I want to access them in the browser tab's console (on Firefox the Web Console ).我想在浏览器选项卡的控制台中访问它们(在 Firefox 上的Web 控制台)。 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.在 Chrome 中,我可以通过按F12打开控制台,然后导航到开发人员工具中的控制台选项卡。 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?在 Firefox 中,如何做同样的事情?

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.似乎不存在将Web 控制台的上下文/范围(使用Ctrl - Shift - KF12直接打开并选择控制台选项卡)更改为扩展内容脚本的上下文/范围的能力。 In addition, this capability does not exist in any of the other ways to view a console in Firefox .此外,在 Firefox 中查看控制台的任何其他方式都不存在此功能。 A bug/RFE should be filed on Bugzilla requesting this functionality;在 Bugzilla上提交请求此功能的错误/RFE 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 (ie the top frame and each child frame).您将希望 RFE 清楚地说明应该能够切换到选项卡中每个框架(即顶部框架和每个子框架)的内容脚本上下文/范围。 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:注意:您可以通过从框架选择器下拉菜单中打开的下拉菜单中选择框架,将控制台更改为 iframe 页面脚本的上下文/范围:

更改为 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".如果此下拉图标没有出现,请转到 DevTools 设置并选中“选择 iframe 作为当前目标文档”。 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).但是,这样做 A) 不会切换到内容脚本上下文/范围,并且 B) 不能与 Web 调试器一起正常工作(在当前版本的 Firefox 和 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.您可以将 Web 调试器( Ctrl - Shift - SF12并选择调试器选项卡)与 WebExtension 内容脚本一起使用。 The content scripts for the extension are listed in the "Sources" under a moz-extension:// URL.扩展的内容脚本列在moz-extension:// URL 下的“来源”中。 You will need to identify the UUID that is used for the extension .您需要确定用于扩展的 UUID 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;放置debugger; directives in the JavaScript which is running in the child iframe is ineffective.在子 iframe 中运行的 JavaScript 中的指令无效。

Web Debugger debugging content script (in top frame): Web Debugger 调试内容脚本(在顶部框架中):

Web Debugger 调试内容脚本(在顶部框架中)

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 .如果您想打开位于 WebExtensions 后台脚本上下文中的控制台,您可以通过单击about:debugging扩展程序的Debug按钮来实现。 However, this will not get you access to a console in the content script's context.但是,这不会让您访问内容脚本上下文中的控制台。

Workarounds for seeing variable values in the iframe在 iframe 中查看变量值的解决方法

For what you need: unambiguously indicating that values are in the iframe context, not the top frame;对于您需要的:明确指出值在 iframe 上下文中,而不是在顶部框架中; 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.使用带有信息的console.log()清楚地表明脚本相信它正在 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.因此,您不必在每次调用console.log()都使用'In iframe' ,您可以创建一个函数,将该文本添加到对该函数的所有调用之前。 You could even override the console.log() function so your code still just calls console.log() .您甚至可以覆盖console.log()函数,这样您的代码仍然只调用console.log()

    However, this only tells you that your code thinks that it is running in the iframe.但是,这只会告诉您您的代码认为它正在 iframe 中运行。 Part of what you may be debugging is your content script code detecting that it is in an iframe.您可能正在调试的部分内容是您的内容脚本代码检测到它在 iframe 中。

    This method does not indicate with certainty that the reported values are actually in the iframe.此方法并不能确定报告的值实际上在 iframe 中。

  • Store values into the DOM using Element.dataset , or other DOM element attributes, and then inspect the DOM for these values.使用Element.dataset或其他 DOM 元素属性将值存储到 DOM 中,然后检查 DOM 中的这些值。 To view these attributes, I find that the DOM Inspector shows these quite clearly:为了查看这些属性,我发现DOM Inspector非常清楚地显示了这些:

    在此处输入图片说明

    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.该方法可用于明确显示值是 iframe 中的值,以及确切显示是哪个 iframe,而无需依赖 iframe 中运行的代码来准确确定它在 iframe 中以及它在哪个 iframe 中。

A simple solution is to just console.log() in the content script and then click the sourcemap link to view the script.一个简单的解决方案是在内容脚本中使用console.log()然后单击源映射链接以查看脚本。 As shown below:如下所示:

在此处输入图片说明

It's not yet possible.这还不可能。 There is a bug Implement UI for switching context to content script opened (since Nov 2017) for that.有一个错误实现 UI,用于将上下文切换到为此打开的内容脚本(自 2017 年 11 月起)。

在 Firefox Developer Edition 中,进入“about:debugging”页面并单击附加组件旁边的“Debug”按钮以打开开发工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM