From 5d3a0bccfb72c1300af0b31bdc8dd53fa3af9e9a Mon Sep 17 00:00:00 2001 From: jay817 Date: Tue, 24 Dec 2024 17:27:07 +0000 Subject: [PATCH] Update playwright/README.md --- playwright/README.md | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/playwright/README.md b/playwright/README.md index edc4c00..8d1661b 100644 --- a/playwright/README.md +++ b/playwright/README.md @@ -5,20 +5,25 @@ Linux (WSL Debian) pip install playwright playwright install --with-deps """ -def sync(f): - import asyncio, threading, queue, functools; asyncio.set_event_loop_policy(None) - def thread(): sync.q.get(); f, a, k = sync.p; sync.p = f(*a, **k); sync.q.task_done(); thread() - if not hasattr(sync, 'q'): sync.q = queue.Queue(); threading.Thread(target=thread).start() - def wrapper(*a, **k): sync.p = f, a, k; sync.q.put(1); sync.q.join(); return sync.p - return functools.wraps(f)(wrapper) -@sync def Page(): - from playwright.sync_api import sync_playwright as P - (context := P().start().firefox.launch().new_context()).set_default_timeout(0) - for attr in dir(page := context.new_page()): - if attr[0] != '_' and callable(f := getattr(page, attr)): setattr(page, attr, sync(f)) - page._repr_png_ = page.screenshot; return page + def sync(f): + import asyncio, threading, queue, functools; asyncio.set_event_loop_policy(None) + def thread(): + while sync.q.get(): f, a, k = sync.p; sync.p = f(*a, **k); sync.q.task_done() + if not hasattr(sync, 'q'): sync.q = queue.Queue(); threading.Thread(target=thread).start() + def wrapper(*a, **k): sync.p = f, a, k; sync.q.put(True); sync.q.join(); return sync.p + return functools.wraps(f)(wrapper) + @sync + def Page(): + from playwright.sync_api import sync_playwright as P + (context := P().start().firefox.launch().new_context()).set_default_timeout(0) + for attr in dir(page := context.new_page()): + if attr[0] != '_' and callable(f := getattr(page, attr)): setattr(page, attr, sync(f)) + page._repr_png_ = page.screenshot + page.goto = lambda url, goto=page.goto: goto(url if '//' in url else f'https://{url}') + return page + return Page() page = Page() -page.goto('https://google.com') +page.goto('google.com') page ``` \ No newline at end of file