docs: fix chrome extension examples (#14968)

This commit is contained in:
Kaspar Emanuel 2022-06-21 03:04:34 +01:00 committed by GitHub
parent 5e6b493bc9
commit 42e6e094ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,7 +7,7 @@ title: "Chrome Extensions"
Extensions only work in Chrome / Chromium in non-headless mode, launched with a persistent context.
:::
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 a [Manifest v2](https://developer.chrome.com/docs/extensions/mv2/) extension whose source is located in `./my-extension`:
```js
const { chromium } = require('playwright');
@ -22,7 +22,10 @@ const { chromium } = require('playwright');
`--load-extension=${pathToExtension}`
]
});
const backgroundPage = browserContext.backgroundPages()[0];
let [backgroundPage] = browserContext.backgroundPages();
if (!backgroundPage)
backgroundPage = await browserContext.waitForEvent('backgroundpage');
// Test the background page as you would any other page.
await browserContext.close();
})();
@ -45,7 +48,12 @@ async def run(playwright):
f"--load-extension={path_to_extension}",
],
)
background_page = context.background_pages[0]
if len(context.background_pages) == 0:
background_page = await context.wait_for_event('backgroundpage')
else:
background_page = context.background_pages[0]
# Test the background page as you would any other page.
await context.close()
@ -74,7 +82,11 @@ def run(playwright):
f"--load-extension={path_to_extension}",
],
)
background_page = context.background_pages[0]
if len(context.background_pages) == 0:
background_page = context.wait_for_event('backgroundpage')
else:
background_page = context.background_pages[0]
# Test the background page as you would any other page.
context.close()