Add-ons

tabs.executeScript()

Injects JavaScript code into a page.

You can inject code into pages whose URL can be expressed using a match pattern: meaning, its scheme must be one of "http", "https", "file", "ftp". To do this you must have the permission for the page's URL, either explicitly as a host permission, or via the activeTab permission.

You can also inject code into pages packaged with your own extension:

browser.tabs.create({url: "/my-page.html"}).then(() => {
  browser.tabs.executeScript({
    code: `console.log('location:', window.location.href);`
  });
});

You don't need any special permissions to do this.

You can't inject code into any of the browser's built-in pages, such as about:debugging, about:addons, or the page that opens when you open a new empty tab.

The scripts you inject are called content scripts. Learn more about content scripts.

This is an asynchronous function that returns a Promise.

Document Tags and Contributors

Last updated by: YurkaninRyan,