Skip to content

Commit

Permalink
Added extensions support
Browse files Browse the repository at this point in the history
  • Loading branch information
kunwar-reworkd committed Dec 18, 2024
1 parent aadf18c commit d0f6d56
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion sdk/harambe/contrib/playwright/harness.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import shutil
import tempfile
from collections import defaultdict
from contextlib import asynccontextmanager
from typing import Any, AsyncGenerator, Awaitable, Callable, Optional, Sequence, cast
Expand Down Expand Up @@ -39,6 +41,7 @@ async def playwright_harness(
browser_type: Optional[BrowserType] = None,
enable_clipboard: bool = False,
launch_args: Sequence[str] = (),
extensions: Sequence[str] = (),
**__: Any,
) -> AsyncGenerator[PageFactory, None]:
"""
Expand All @@ -47,13 +50,27 @@ async def playwright_harness(
creation of HAR file, and stealth.
"""
async with async_playwright() as p:
extension_args = []
user_data_dir = tempfile.mkdtemp()
browser_type = browser_type or "chromium"

if extensions and browser_type == "chromium":
extension_paths = ",".join(extensions)
extension_args.extend(
[
f"--disable-extensions-except={extension_paths}",
f"--load-extension={extension_paths}",
]
)

browser = await (
p.chromium.connect_over_cdp(endpoint_url=cdp_endpoint)
if cdp_endpoint
else getattr(p, cast(str, browser_type or "chromium")).launch(
else getattr(p, cast(str, browser_type)).launch(
headless=headless,
args=[
*launch_args,
*extension_args,
*(
# Disables navigator.webdriver showing up
["--disable-blink-features=AutomationControlled"]
Expand Down Expand Up @@ -135,3 +152,4 @@ async def page_factory(*_: Any, **__: Any) -> PlaywrightPage:
finally:
await ctx.close()
await browser.close()
shutil.rmtree(user_data_dir, ignore_errors=True)

0 comments on commit d0f6d56

Please sign in to comment.