如何在 Chrome JS 控制台中进入内容脚本的沙盒环境
如果我在 Chrome 开发者工具中打开 JavaScript 控制台来调试扩展程序的内容脚本,我将无法获取内容脚本的上下文。例如,jQuery 不可访问,并且除非进入调试器并设置断点,否则我无法访问全局变量。
我只是错过了什么吗?如果能够从 JS 控制台检查我的全局变量或调用 jQuery,那就太好了。
If I open up the JavaScript console in Chrome Developer Tools to debug my extension's content scripts, I don't get the context of the content scripts. For example, jQuery isn't accessible, and I can't get access to my global variables unless I go to the debugger and set up a breakpoint.
Am I just missing something? It would be great to be able to check my global variables from the JS console or invoke jQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前不可能在内容脚本的上下文中执行评估,除了所描述的设置断点/插入调试器语句并在脚本内暂停的方法之外。我对此提交了一个 bug,您可以将自己添加到 CC 列表中进行跟踪它的进步。
It is not possible at the moment to perform evaluations in context of a content script except the described way of setting a breakpoint/inserting debugger statement and pausing inside the script. I filed a bug on this, You can add yourself to the CC list to track its progress.
您可以通过在内容脚本的隔离世界中触发调试器来间接实现此目的:
chrome.tabs.executeScript(undefined, {'code': 'debugger'})
您还应该能够直接在您的代码中使用
debugger
关键字内容脚本,如果您希望检查执行中的某个位置。You can achieve this indirectly by triggering the debugger in the isolated world of the content script:
chrome.tabs.executeScript(undefined, {'code': 'debugger'})
You should also be able to use the
debugger
keyword directly in your content script, if there's a place in the execution that you wish to inspect.