From 42e6e094ca9f3a8ecb939ffcb4c604fd6e8f996e Mon Sep 17 00:00:00 2001 From: Kaspar Emanuel Date: Tue, 21 Jun 2022 03:04:34 +0100 Subject: [PATCH] docs: fix chrome extension examples (#14968) --- docs/src/chrome-extensions-js-python.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/docs/src/chrome-extensions-js-python.md b/docs/src/chrome-extensions-js-python.md index 5368fa5fa8..507ec17152 100644 --- a/docs/src/chrome-extensions-js-python.md +++ b/docs/src/chrome-extensions-js-python.md @@ -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()