Use chrome.devtools.inspectedWindow
to interact with the inspected window:
obtain the tab ID for the inspected page,
evaluate the code in the context of inspected window,
reload the page,
or obtain the list of resources within the page.
See DevTools APIs summary for general introduction to using Developer Tools APIs.
The tabId
property
provides the tab identifier that you can use with the
chrome.tabs.*
API calls.
However, please note that chrome.tabs.*
API is not
exposed to the Developer Tools extension pages due to security considerations
— you will need to pass the tab ID to the background page and invoke
the chrome.tabs.*
API functions from there.
The eval()
method provides the ability for extensions to execute
JavaScript code in the context of the main frame of the inspected page.
This method is powerful when used in the right context
and dangerous when used inappropriately.
Use the chrome.tabs.executeScript()
method
unless you need the specific functionality
that the eval()
method provides.
Here are the main differences between the
eval()
and chrome.tabs.executeScript()
methods:
eval()
method does not
use an isolated world for the code being evaluated, so the JavaScript state
of the inspected window is accessible to the code.
Use this method when access to the JavaScript state of the inspected page
is required.
inspect()
and $0
.
Important:
Due to the security considerations explained above, the
chrome.tabs.executeScript()
method is the preferred way for an
extension to access DOM data of the inspected page in cases where the access to
JavaScript state of the inspected page is not required.
The reload()
method may be used to reload the inspected page.
Additionally, the caller can specify an override for the user agent string,
a script that will be injected early upon page load, and an option to force
reload of cached resources.
Use the getResources()
call and the onResourceContent
event to obtain the list of resources (documents, stylesheets, scripts, images
etc) within the inspected page. The getContent()
and setContent()
methods of the Resource
class along with the
onResourceContentCommitted
event may be used to support
modification of the resource content, for example, by an external editor.
The following code checks for the version of jQuery used by the inspected page:
chrome.devtools.inspectedWindow.eval( "jQuery.fn.jquery", function(result, isException) { if (isException) console.log("the page is not using jQuery"); else console.log("The page is using jQuery v" + result); } );
You can find more examples that use Developer Tools APIs in Samples.