pub struct WindowIpc { /* private fields */ }Expand description
The Window’s IPC interface, used for evalling, events, etc.
Implementations§
Source§impl WindowIpc
impl WindowIpc
pub fn new(ipc: Arc<Mutex<Option<BrowserIpc>>>) -> Self
Sourcepub fn block_until_initialized(&self) -> Result<(), CrowserError>
pub fn block_until_initialized(&self) -> Result<(), CrowserError>
Wait until the IPC layer has made a connection with the window.
Examples found in repository?
examples/javascript.rs (line 17)
3fn main() -> Result<(), CrowserError> {
4 let mut profile_dir = std::env::current_dir()?;
5 profile_dir.push("example_profiles");
6
7 let config = RemoteConfig {
8 url: "https://example.com".to_string(),
9 };
10
11 let mut window = Window::new(config, None, profile_dir)?;
12 let ipc = window.ipc();
13
14 window.clear_profile().unwrap_or_default();
15
16 std::thread::spawn(move || {
17 ipc.block_until_initialized().unwrap_or_default();
18
19 let two = ipc.eval("1 + 1").unwrap();
20 println!("1 + 1 = {:?}", two);
21
22 std::thread::sleep(std::time::Duration::from_secs(2));
23
24 ipc.eval("alert('Hello from Crowser!')").unwrap_or_default();
25 });
26
27 window.create()?;
28
29 Ok(())
30}More examples
examples/commands.rs (line 17)
3fn main() -> Result<(), CrowserError> {
4 let mut profile_dir = std::env::current_dir()?;
5 profile_dir.push("example_profiles");
6
7 let config = RemoteConfig {
8 url: "https://example.com".to_string(),
9 };
10
11 let mut window = Window::new(config, None, profile_dir)?;
12 let ipc = window.ipc();
13
14 window.clear_profile().unwrap_or_default();
15
16 std::thread::spawn(move || {
17 ipc.block_until_initialized().unwrap_or_default();
18
19 ipc
20 .register_command("hello", |_| {
21 println!("Got hello command");
22 Ok(serde_json::json!("Hello from Crowser!"))
23 })
24 .unwrap_or_default();
25
26 std::thread::sleep(std::time::Duration::from_secs(1));
27
28 // Eval some JS that calls that command
29 let result = ipc
30 .eval("window.__CROWSER.ipc.invoke('hello')")
31 .unwrap_or_default();
32 println!("Result: {:?}", result);
33 });
34
35 window.create()?;
36
37 Ok(())
38}Sourcepub fn eval(&self, script: impl AsRef<str>) -> Result<Value, CrowserError>
pub fn eval(&self, script: impl AsRef<str>) -> Result<Value, CrowserError>
Eval JavaScript in the window.
Examples found in repository?
examples/javascript.rs (line 19)
3fn main() -> Result<(), CrowserError> {
4 let mut profile_dir = std::env::current_dir()?;
5 profile_dir.push("example_profiles");
6
7 let config = RemoteConfig {
8 url: "https://example.com".to_string(),
9 };
10
11 let mut window = Window::new(config, None, profile_dir)?;
12 let ipc = window.ipc();
13
14 window.clear_profile().unwrap_or_default();
15
16 std::thread::spawn(move || {
17 ipc.block_until_initialized().unwrap_or_default();
18
19 let two = ipc.eval("1 + 1").unwrap();
20 println!("1 + 1 = {:?}", two);
21
22 std::thread::sleep(std::time::Duration::from_secs(2));
23
24 ipc.eval("alert('Hello from Crowser!')").unwrap_or_default();
25 });
26
27 window.create()?;
28
29 Ok(())
30}More examples
examples/commands.rs (line 30)
3fn main() -> Result<(), CrowserError> {
4 let mut profile_dir = std::env::current_dir()?;
5 profile_dir.push("example_profiles");
6
7 let config = RemoteConfig {
8 url: "https://example.com".to_string(),
9 };
10
11 let mut window = Window::new(config, None, profile_dir)?;
12 let ipc = window.ipc();
13
14 window.clear_profile().unwrap_or_default();
15
16 std::thread::spawn(move || {
17 ipc.block_until_initialized().unwrap_or_default();
18
19 ipc
20 .register_command("hello", |_| {
21 println!("Got hello command");
22 Ok(serde_json::json!("Hello from Crowser!"))
23 })
24 .unwrap_or_default();
25
26 std::thread::sleep(std::time::Duration::from_secs(1));
27
28 // Eval some JS that calls that command
29 let result = ipc
30 .eval("window.__CROWSER.ipc.invoke('hello')")
31 .unwrap_or_default();
32 println!("Result: {:?}", result);
33 });
34
35 window.create()?;
36
37 Ok(())
38}Sourcepub fn listen(
&self,
name: impl AsRef<str>,
callback: fn(Value) -> Result<Value, CrowserError>,
) -> Result<(), CrowserError>
pub fn listen( &self, name: impl AsRef<str>, callback: fn(Value) -> Result<Value, CrowserError>, ) -> Result<(), CrowserError>
Listen for events from the window.
Sourcepub fn register_command(
&self,
name: impl AsRef<str>,
callback: fn(Value) -> Result<Value, CrowserError>,
) -> Result<(), CrowserError>
pub fn register_command( &self, name: impl AsRef<str>, callback: fn(Value) -> Result<Value, CrowserError>, ) -> Result<(), CrowserError>
Register commands that the Window can emit.
Examples found in repository?
examples/commands.rs (lines 20-23)
3fn main() -> Result<(), CrowserError> {
4 let mut profile_dir = std::env::current_dir()?;
5 profile_dir.push("example_profiles");
6
7 let config = RemoteConfig {
8 url: "https://example.com".to_string(),
9 };
10
11 let mut window = Window::new(config, None, profile_dir)?;
12 let ipc = window.ipc();
13
14 window.clear_profile().unwrap_or_default();
15
16 std::thread::spawn(move || {
17 ipc.block_until_initialized().unwrap_or_default();
18
19 ipc
20 .register_command("hello", |_| {
21 println!("Got hello command");
22 Ok(serde_json::json!("Hello from Crowser!"))
23 })
24 .unwrap_or_default();
25
26 std::thread::sleep(std::time::Duration::from_secs(1));
27
28 // Eval some JS that calls that command
29 let result = ipc
30 .eval("window.__CROWSER.ipc.invoke('hello')")
31 .unwrap_or_default();
32 println!("Result: {:?}", result);
33 });
34
35 window.create()?;
36
37 Ok(())
38}Auto Trait Implementations§
impl Freeze for WindowIpc
impl RefUnwindSafe for WindowIpc
impl Send for WindowIpc
impl Sync for WindowIpc
impl Unpin for WindowIpc
impl UnwindSafe for WindowIpc
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more