docs: fix chrome extension examples (#14968)
This commit is contained in:
parent
5e6b493bc9
commit
42e6e094ca
|
|
@ -7,7 +7,7 @@ title: "Chrome Extensions"
|
||||||
Extensions only work in Chrome / Chromium in non-headless mode, launched with a persistent context.
|
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
|
```js
|
||||||
const { chromium } = require('playwright');
|
const { chromium } = require('playwright');
|
||||||
|
|
@ -22,7 +22,10 @@ const { chromium } = require('playwright');
|
||||||
`--load-extension=${pathToExtension}`
|
`--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.
|
// Test the background page as you would any other page.
|
||||||
await browserContext.close();
|
await browserContext.close();
|
||||||
})();
|
})();
|
||||||
|
|
@ -45,7 +48,12 @@ async def run(playwright):
|
||||||
f"--load-extension={path_to_extension}",
|
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.
|
# Test the background page as you would any other page.
|
||||||
await context.close()
|
await context.close()
|
||||||
|
|
||||||
|
|
@ -74,7 +82,11 @@ def run(playwright):
|
||||||
f"--load-extension={path_to_extension}",
|
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.
|
# Test the background page as you would any other page.
|
||||||
context.close()
|
context.close()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue