auto close when page number exceeds (#4101)

This commit is contained in:
LawyZheng
2025-11-26 14:37:14 +08:00
committed by GitHub
parent e692ae8944
commit 1fff8a2ac6
2 changed files with 19 additions and 3 deletions

View File

@@ -119,6 +119,7 @@ class Settings(BaseSettings):
BROWSER_HEIGHT: int = 1080
BROWSER_POLICY_FILE: str = "/etc/chromium/policies/managed/policies.json"
BROWSER_LOGS_ENABLED: bool = True
BROWSER_MAX_PAGES_NUMBER: int = 10
# Add extension folders name here to load extension in your browser
EXTENSIONS_BASE_PATH: str = "./extensions"

View File

@@ -781,14 +781,14 @@ class BrowserState:
await self.set_working_page(last_page, len(pages) - 1)
return last_page
async def list_valid_pages(self) -> list[Page]:
# List all valid pages(blank page, and http/https page) in the browser context
async def list_valid_pages(self, max_pages: int = settings.BROWSER_MAX_PAGES_NUMBER) -> list[Page]:
# List all valid pages(blank page, and http/https page) in the browser context, up to max_pages
# MSEdge CDP bug(?)
# when using CDP connect to a MSEdge, the download hub will be included in the context.pages
if self.browser_context is None:
return []
return [
pages = [
http_page
for http_page in self.browser_context.pages
if (
@@ -798,6 +798,21 @@ class BrowserState:
)
]
if max_pages <= 0 or len(pages) <= max_pages:
return pages
reserved_pages = pages[-max_pages:]
closing_pages = pages[: len(pages) - max_pages]
LOG.warning(
"The page number exceeds the limit, closing the oldest pages. It might cause the video missing",
closing_pages=closing_pages,
)
for page in closing_pages:
await page.close()
return reserved_pages
async def validate_browser_context(self, page: Page) -> bool:
# validate the content
try: