Skip to content

Commit

Permalink
docs: improve the Working with Chrome Extensions section (#8321)
Browse files Browse the repository at this point in the history
- Added a note to mention the experimental Chrome headless mode can
  also be used.
- Improved the example code to wait for the background page to be
  ready. The previous example sometimes didn't work, since the
  background page would take a split-second to load.
- Added a note to mention how to find the background ServiceWorker for
  Chrome MV3 extensions.
  • Loading branch information
kzar committed May 10, 2022
1 parent 7060d97 commit 1e82d98
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions docs/api.md
Expand Up @@ -496,7 +496,7 @@ If Puppeteer doesn't find them in the environment during the installation step,

Puppeteer can be used for testing Chrome Extensions.

> **NOTE** Extensions in Chrome / Chromium currently only work in non-headless mode.
> **NOTE** Extensions in Chrome / Chromium currently only work in non-headless mode and experimental Chrome headless mode.
The following is code for getting a handle to the [background page](https://developer.chrome.com/extensions/background_pages) of an extension whose source is located in `./my-extension`:

Expand All @@ -506,14 +506,13 @@ const puppeteer = require('puppeteer');
(async () => {
const pathToExtension = require('path').join(__dirname, 'my-extension');
const browser = await puppeteer.launch({
headless: false,
headless: 'chrome',
args: [
`--disable-extensions-except=${pathToExtension}`,
`--load-extension=${pathToExtension}`,
],
});
const targets = await browser.targets();
const backgroundPageTarget = targets.find(
const backgroundPageTarget = await browser.waitForTarget(
(target) => target.type() === 'background_page'
);
const backgroundPage = await backgroundPageTarget.page();
Expand All @@ -522,6 +521,8 @@ const puppeteer = require('puppeteer');
})();
```

> **NOTE** Chrome Manifest V3 extensions have a background ServiceWorker of type 'service_worker', instead of a page of type 'background_page'.
> **NOTE** It is not yet possible to test extension popups or content scripts.
### class: Puppeteer
Expand Down

0 comments on commit 1e82d98

Please sign in to comment.