4949 from_nullable_channel ,
5050)
5151from playwright ._impl ._console_message import ConsoleMessage
52+ from playwright ._impl ._disposable import Disposable , DisposableStub
5253from playwright ._impl ._download import Download
5354from playwright ._impl ._element_handle import ElementHandle , determine_screenshot_type
5455from playwright ._impl ._errors import Error , TargetClosedError , is_target_closed_error
@@ -501,23 +502,25 @@ async def add_style_tag(
501502 ) -> ElementHandle :
502503 return await self ._main_frame .add_style_tag (** locals_to_params (locals ()))
503504
504- async def expose_function (self , name : str , callback : Callable ) -> None :
505- await self .expose_binding (name , lambda source , * args : callback (* args ))
505+ async def expose_function (self , name : str , callback : Callable ) -> Disposable :
506+ return await self .expose_binding (name , lambda source , * args : callback (* args ))
506507
507508 async def expose_binding (
508509 self , name : str , callback : Callable , handle : bool = None
509- ) -> None :
510+ ) -> Disposable :
510511 if name in self ._bindings :
511512 raise Error (f'Function "{ name } " has been already registered' )
512513 if name in self ._browser_context ._bindings :
513514 raise Error (
514515 f'Function "{ name } " has been already registered in the browser context'
515516 )
516517 self ._bindings [name ] = callback
517- await self ._channel .send (
518- "exposeBinding" ,
519- None ,
520- dict (name = name , needsHandle = handle or False ),
518+ return from_channel (
519+ await self ._channel .send (
520+ "exposeBinding" ,
521+ None ,
522+ dict (name = name , needsHandle = handle or False ),
523+ )
521524 )
522525
523526 async def set_extra_http_headers (self , headers : Dict [str , str ]) -> None :
@@ -661,18 +664,20 @@ async def bring_to_front(self) -> None:
661664
662665 async def add_init_script (
663666 self , script : str = None , path : Union [str , Path ] = None
664- ) -> None :
667+ ) -> Disposable :
665668 if path :
666669 script = add_source_url_to_script (
667670 (await async_readfile (path )).decode (), path
668671 )
669672 if not isinstance (script , str ):
670673 raise Error ("Either path or script parameter must be specified" )
671- await self ._channel .send ("addInitScript" , None , dict (source = script ))
674+ return from_channel (
675+ await self ._channel .send ("addInitScript" , None , dict (source = script ))
676+ )
672677
673678 async def route (
674679 self , url : URLMatch , handler : RouteHandlerCallback , times : int = None
675- ) -> None :
680+ ) -> DisposableStub :
676681 self ._routes .insert (
677682 0 ,
678683 RouteHandler (
@@ -684,6 +689,7 @@ async def route(
684689 ),
685690 )
686691 await self ._update_interception_patterns ()
692+ return DisposableStub (lambda : self .unroute (url , handler ), self )
687693
688694 async def unroute (
689695 self , url : URLMatch , handler : Optional [RouteHandlerCallback ] = None
0 commit comments