mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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:
parent
7060d97b66
commit
1e82d98027
@ -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.
|
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`:
|
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 () => {
|
(async () => {
|
||||||
const pathToExtension = require('path').join(__dirname, 'my-extension');
|
const pathToExtension = require('path').join(__dirname, 'my-extension');
|
||||||
const browser = await puppeteer.launch({
|
const browser = await puppeteer.launch({
|
||||||
headless: false,
|
headless: 'chrome',
|
||||||
args: [
|
args: [
|
||||||
`--disable-extensions-except=${pathToExtension}`,
|
`--disable-extensions-except=${pathToExtension}`,
|
||||||
`--load-extension=${pathToExtension}`,
|
`--load-extension=${pathToExtension}`,
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
const targets = await browser.targets();
|
const backgroundPageTarget = await browser.waitForTarget(
|
||||||
const backgroundPageTarget = targets.find(
|
|
||||||
(target) => target.type() === 'background_page'
|
(target) => target.type() === 'background_page'
|
||||||
);
|
);
|
||||||
const backgroundPage = await backgroundPageTarget.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.
|
> **NOTE** It is not yet possible to test extension popups or content scripts.
|
||||||
|
|
||||||
### class: Puppeteer
|
### class: Puppeteer
|
||||||
|
Loading…
Reference in New Issue
Block a user