From 1e82d980274a149eb37c5f338a74377c6692ae1c Mon Sep 17 00:00:00 2001 From: Dave Vandyke Date: Tue, 10 May 2022 07:21:57 +0100 Subject: [PATCH] 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. --- docs/api.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/api.md b/docs/api.md index a4efe64488fc8..23f5c80b82fff 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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