mirror of
https://forge.fsky.io/oneflux/omegafox.git
synced 2026-02-10 04:42:03 -08:00
- Python library now constrains the supported Camoufox version, and will force an update if you are out of date. - Added support patch for multiple accepted languages #37 - Added pysocks #43 - Added README deprecation notices - Added public launch_options command - Bumped python library to 0.3.0 - Full support for beta.12
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
from typing import Any, Dict, Optional, Union
|
|
|
|
from playwright.async_api import (
|
|
Browser,
|
|
BrowserContext,
|
|
Playwright,
|
|
PlaywrightContextManager,
|
|
)
|
|
|
|
from .utils import launch_options
|
|
|
|
|
|
class AsyncCamoufox(PlaywrightContextManager):
|
|
"""
|
|
Wrapper around playwright.async_api.PlaywrightContextManager that automatically
|
|
launches a browser and closes it when the context manager is exited.
|
|
"""
|
|
|
|
def __init__(self, **launch_options):
|
|
super().__init__()
|
|
self.launch_options = launch_options
|
|
self.browser: Optional[Union[Browser, BrowserContext]] = None
|
|
|
|
async def __aenter__(self) -> Union[Browser, BrowserContext]:
|
|
_playwright = await super().__aenter__()
|
|
self.browser = await AsyncNewBrowser(_playwright, **self.launch_options)
|
|
return self.browser
|
|
|
|
async def __aexit__(self, *args: Any):
|
|
if self.browser:
|
|
await self.browser.close()
|
|
await super().__aexit__(*args)
|
|
|
|
|
|
async def AsyncNewBrowser(
|
|
playwright: Playwright,
|
|
*,
|
|
from_options: Optional[Dict[str, Any]] = None,
|
|
persistent_context: bool = False,
|
|
**kwargs,
|
|
) -> Union[Browser, BrowserContext]:
|
|
"""
|
|
Launches a new browser instance for Camoufox given a set of launch options.
|
|
|
|
Parameters:
|
|
from_options (Dict[str, Any]):
|
|
A set of launch options generated by `launch_options()` to use
|
|
persistent_context (bool):
|
|
Whether to use a persistent context.
|
|
**kwargs:
|
|
All other keyword arugments passed to `launch_options()`.
|
|
"""
|
|
opt = launch_options(**kwargs)
|
|
|
|
if persistent_context:
|
|
return await playwright.firefox.launch_persistent_context(**opt)
|
|
|
|
return await playwright.firefox.launch(**opt)
|