tabs.executeScript()

Injects JavaScript code into a page. For details, see the programmatic injection section of the content scripts doc.

Syntax

browser.tabs.executeScript(
  tabId,                 // optional integer
  details,               // extensionTypes.InjectDetails
  function(result) {...} // optional function
)

Parameters

tabIdOptional
integer. The ID of the tab in which to run the script; defaults to the active tab of the current window.
details
extensionTypes.InjectDetails. Details of the script to run.

Values of this type are objects. They contain the following properties:

codeOptional
string. JavaScript or CSS code to inject.

Warning:
Be careful using the code parameter. Incorrect use of it may open your extension to cross-site scripting attacks.
fileOptional
string. JavaScript or CSS file to inject.
allFramesOptional
boolean. If allFrames is true, implies that the JavaScript or CSS should be injected into all frames of the current page. By default, it's false and is only injected into the top frame.
matchAboutBlankOptional
boolean. If matchAboutBlank is true, then the code is also injected in about:blank and about:srcdoc frames if your extension has access to its parent document. The code cannot be inserted in top-level about:-frames. By default it is false.
runAtOptional
extensionTypes.RunAt. The soonest that the JavaScript or CSS will be injected into the tab. Defaults to "document_idle".
callbackOptional Requires Gecko 47
function. Called after all the JavaScript has been executed.The function is passed the following arguments:
resultOptional
array of any. The result of the script in every injected frame.

Browser compatibility

EdgeFirefoxChromeOpera
Basic support?43.0Yes33
Firefox
Basic supportNo

Examples

Example add-ons

Acknowledgements

This API is based on Chromium's chrome.tabs API. This documentation is derived from tabs.json in the Chromium code.

Document Tags and Contributors

 Contributors to this page: wbamberg, rolfedh, kmaglione
 Last updated by: wbamberg,