docs: improve the Working with Chrome Extensions section (#8321)

- 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.
This commit is contained in:
Dave Vandyke 2022-05-10 07:21:57 +01:00 committed by GitHub
parent 7060d97b66
commit 1e82d98027
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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`:
@ -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();
@ -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