Update playwright/README.md
This commit is contained in:
@@ -7,29 +7,19 @@ playwright install --with-deps
|
|||||||
"""
|
"""
|
||||||
def Page():
|
def Page():
|
||||||
def sync(func):
|
def sync(func):
|
||||||
if not hasattr(sync, 'call'):
|
import asyncio, threading, queue, functools; asyncio.set_event_loop_policy(None)
|
||||||
sync.call = __import__('queue').Queue()
|
def server():
|
||||||
def server():
|
while sync.q.get(): f, a, kw = sync.obj; sync.obj = f(*a, **kw); sync.q.task_done()
|
||||||
while sync.call.get():
|
if not hasattr(sync, 'q'): sync.q = queue.Queue(); threading.Thread(target=server).start()
|
||||||
func, args, kwargs = sync.obj
|
@functools.wraps(func)
|
||||||
sync.obj = func(*args, **kwargs)
|
def wrapper(*a, **kw): sync.obj = func, a, kw; sync.q.put(True); sync.q.join(); return sync.obj
|
||||||
sync.call.task_done()
|
|
||||||
__import__('asyncio').set_event_loop_policy(None)
|
|
||||||
__import__('threading').Thread(target=server).start()
|
|
||||||
@__import__('functools').wraps(func)
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
sync.obj = func, args, kwargs
|
|
||||||
sync.call.put(True); sync.call.join()
|
|
||||||
return sync.obj
|
|
||||||
return wrapper
|
return wrapper
|
||||||
@sync
|
@sync
|
||||||
def Page():
|
def Page():
|
||||||
from playwright.sync_api import sync_playwright
|
from playwright.sync_api import sync_playwright
|
||||||
browser = sync_playwright().start().firefox.launch()
|
(context := sync_playwright().start().firefox.launch().new_context()).set_default_timeout(0)
|
||||||
(context := browser.new_context()).set_default_timeout(0)
|
|
||||||
for attr in dir(page := context.new_page()):
|
for attr in dir(page := context.new_page()):
|
||||||
if attr[0] != '_' and callable(method := getattr(page, attr)):
|
if attr[0] != '_' and callable(method := getattr(page, attr)): setattr(page, attr, sync(method))
|
||||||
setattr(page, attr, sync(method))
|
|
||||||
page._repr_png_ = page.screenshot
|
page._repr_png_ = page.screenshot
|
||||||
return page
|
return page
|
||||||
return Page()
|
return Page()
|
||||||
|
|||||||
Reference in New Issue
Block a user