Injects JavaScript code into a page.
You can inject code into pages whose URL can be expressed using a match pattern. To do so, its scheme must be one of: http
, https
, or file
.
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 cannot 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.
This is an asynchronous function that returns a Promise
.