pub struct Worker { /* private fields */ }Expand description
Worker represents a Web Worker or Service Worker.
Workers are created by the page using the Worker constructor or by browsers
for registered service workers. They run JS in an isolated global scope.
§Example
use playwright_rs::protocol::Playwright;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let playwright = Playwright::launch().await?;
let browser = playwright.chromium().launch().await?;
let page = browser.new_page().await?;
page.on_worker(|worker| {
println!("Worker created: {}", worker.url());
Box::pin(async move { Ok(()) })
}).await?;
browser.close().await?;
Ok(())
}Implementations§
Source§impl Worker
impl Worker
Sourcepub fn new(
parent: Arc<dyn ChannelOwner>,
type_name: String,
guid: Arc<str>,
initializer: Value,
) -> Result<Self>
pub fn new( parent: Arc<dyn ChannelOwner>, type_name: String, guid: Arc<str>, initializer: Value, ) -> Result<Self>
Creates a new Worker from protocol initialization.
Called by the object factory when the server sends a __create__ message
for a Worker object.
Sourcepub fn url(&self) -> &str
pub fn url(&self) -> &str
Returns the URL of this worker.
See: https://playwright.dev/docs/api/class-worker#worker-url
Sourcepub async fn evaluate<R, T>(
&self,
expression: &str,
arg: Option<T>,
) -> Result<R>where
R: DeserializeOwned,
T: Serialize,
pub async fn evaluate<R, T>(
&self,
expression: &str,
arg: Option<T>,
) -> Result<R>where
R: DeserializeOwned,
T: Serialize,
Evaluates a JavaScript expression in the worker context.
The expression is evaluated in the worker’s global scope. Returns the
JSON-serializable result deserialized into type R.
§Arguments
expression- JavaScript expression or function bodyarg- Optional argument to pass to the expression
§Errors
Returns an error if the JavaScript expression throws.
See: https://playwright.dev/docs/api/class-worker#worker-evaluate
Sourcepub async fn evaluate_handle(&self, expression: &str) -> Result<Arc<JSHandle>>
pub async fn evaluate_handle(&self, expression: &str) -> Result<Arc<JSHandle>>
Evaluates a JavaScript expression in the worker context, returning a JSHandle.
Unlike evaluate which deserializes the result,
this returns a live handle to the in-worker JavaScript object.
§Arguments
expression- JavaScript expression or function body
See: https://playwright.dev/docs/api/class-worker#worker-evaluate-handle