Skip to content

chrome.tabs.executeScript API

chrome.tabs.executeScript is a [[Chrome Extension API|chrome-plugin-develop]] method used to programmatically inject and execute JavaScript code into the context of a specific tab or page^[600-developer__frontend__google__chrome-plugin-develop.md].

Functionality

This API allows an extension to run scripts directly in the active page, distinct from the extension's background scripts or popup logic^[600-developer__frontend__google__chrome-plugin-develop.md]. It is commonly utilized to interact with the page's DOM, such as populating form fields or simulating user interactions^[600-developer__frontend__google__chrome-plugin-develop.md].

Implementation

The method requires the target tab's id as the first argument^[600-developer__frontend__google__chrome-plugin-develop.md].

The code to be executed is passed as an object in the second argument, specifically via the code property^[600-developer__frontend__google__chrome-plugin-develop.md]. For example, to automatically fill in login credentials, one might pass a string that sets the value of input elements and triggers a click event^[600-developer__frontend__google__chrome-plugin-develop.md].

Context

This function is typically triggered by user interactions within the extension's interface, such as a button in a popup (popup.html)^[600-developer__frontend__google__chrome-plugin-develop.md]. While the popup handles the UI logic, chrome.tabs.executeScript serves as the bridge to perform actions on the current web page content^[600-developer__frontend__google__chrome-plugin-develop.md].

  • [[Chrome Plugin]]
  • [[Manifest.json]]

Sources