From 188b3c85aeea607aac7189b8b7cced86404dacc6 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Mon, 28 Apr 2025 14:06:31 -0600 Subject: [PATCH] Force open links in current page to stay in operator page context Previously some link clicks would open in new tab. This is out of the browser operator's context and so the new page cannot be interacted with by the browser operator. This change catches new page opens and opens them in the context page instead. --- .../processor/operator/browser_operator.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/khoj/processor/operator/browser_operator.py b/src/khoj/processor/operator/browser_operator.py index 351d0e45..fa53e87a 100644 --- a/src/khoj/processor/operator/browser_operator.py +++ b/src/khoj/processor/operator/browser_operator.py @@ -124,9 +124,25 @@ async def start_browser(width: int = 1024, height: int = 768): launch_args = [f"--window-size={width},{height}", "--disable-extensions", "--disable-file-system"] browser = await playwright.chromium.launch(chromium_sandbox=True, headless=False, args=launch_args, env={}) + # Get the initial browser, page or create one if none exist default_context = browser.contexts[0] if browser.contexts else await browser.new_context() - page = default_context.pages[0] if default_context.pages else await default_context.new_page() + + # Define a handler for new pages + async def handle_new_page(new_page: Page): + # Get the target URL of the new page + target_url = new_page.url + # Close the new page if it is not closed + if not new_page.is_closed(): + await new_page.close() + # Open the target url in the current page instead + if target_url and target_url != "about:blank": + logger.debug(f"Load {target_url} in current page instead of new tab to stay in operator context.") + await page.goto(target_url) + + # Listen for new pages being created in the context + default_context.on("page", handle_new_page) + # If page url is blank, navigate to DuckDuckGo if page.url == "about:blank": await page.goto("https://duckduckgo.com")