Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get to sandboxed environment of content script in Chrome JS console

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.

like image 415
huyz Avatar asked Oct 24 '22 03:10

huyz


2 Answers

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.

like image 101
Yury Semikhatsky Avatar answered Nov 09 '22 10:11

Yury Semikhatsky


You can achieve this indirectly by triggering the debugger in the isolated world of the content script:

  1. Select the tab whose content scripts you wish to inspect
  2. Open the dev tools for that tab
  3. Open the inspector for your extension's background page (or any other extension page) in a popup window
  4. Run 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.

like image 27
Mihai Parparita Avatar answered Nov 09 '22 09:11

Mihai Parparita