Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use set_variable? #133

Open
thecodrr opened this issue Feb 20, 2022 · 8 comments
Open

How to use set_variable? #133

thecodrr opened this issue Feb 20, 2022 · 8 comments
Labels

Comments

@thecodrr
Copy link

thecodrr commented Feb 20, 2022

I tried doing this:

    window
        .set_variable("hello", Value::from("world"))
        .expect("Could not set variable.");

But I don't know how to retrieve the set variable. Window.this.hello and globalThis.hello both return undefined. Even get_variable returns undefined.

@pravic
Copy link
Member

pravic commented Feb 21, 2022

	// set the global to all windows
	if let Err(e) = sciter::set_variable("svglobal", Value::from("hello, globals")) {
		// ignore this
		eprintln!("sciter.set_variable: {:?}", e);
	}

	frame.load_html(html, Some("example://minimal.htm"));

	// set the global only for this window
	if let Err(e) = frame.set_variable("svdocument", Value::from("hello, document")) {
		eprintln!("frame.set_variable: {:?}", e);
	}
globalThis.svdocument;
globalThis.svglobal;

Also, ignore the first error: https://sciter.com/forums/topic/hruntimeset_global_variable-always-returns-false/

@pravic
Copy link
Member

pravic commented Feb 21, 2022

And get:

	println!("document: {:?}", frame.get_variable("svglobal"));
	println!("document: {:?}", frame.get_variable("svdocument"));
	println!("location: {:?}", frame.get_variable("location"));

	// doesn't work:
	println!("Window.this: {:?}", frame.get_variable("Window.this"));

@pravic
Copy link
Member

pravic commented Feb 21, 2022

Keep in mind that this API was introduced (yet disabled) in Sciter 4.4.4.6. but properly enabled only in 4.4.8.26.
Available in rust-sciter since 0.5.58, see 8507e91.

@thecodrr
Copy link
Author

thecodrr commented Feb 21, 2022

Tried doing as you suggested. No luck.

    sciter::set_library("./lib/libsciter-gtk.so").expect("Invalid library path.");

    println!("Version: {:?}", sciter::version());

    let mut frame = sciter::Window::new();
    // let mut root = frame.get_host().get_root().expect("hello");

    // set the global to all windows
    if let Err(e) = sciter::set_variable("svglobal", Value::from("hello, globals")) {
        // ignore this
        eprintln!("sciter.set_variable: {:?}", e);
    }

    frame.load_file("./host/main.htm");

    // set the global only for this window
    if let Err(e) = frame.set_variable("svdocument", Value::from("hello, document")) {
        eprintln!("frame.set_variable: {:?}", e);
    }

    println!("document: {:?}", frame.get_variable("svglobal"));
    println!("document: {:?}", frame.get_variable("svdocument"));
    println!("location: {:?}", frame.get_variable("location"));

    frame.run_app();

Result:

Version: "4.4.8.28"
INFO:TIS: HELLO!,undefined,undefined
frame.set_variable: ()
document: Err(())
document: Err(())
location: Err(())

The JS part:

console.log("HELLO!", globalThis.svdocument, globalThis.svglobal);

Edit: oh wait. From the output, i can see the variables are getting set after the initialization. Let me put this into an event handler.

@pravic
Copy link
Member

pravic commented Feb 22, 2022

From the output, i can see the variables are getting set after the initialization. Let me put this into an event handler

Well, on Windows load_file is synchronous, but it may differ on Linux. Any luck?

@thecodrr
Copy link
Author

No, it's sync everywhere. The load_file function loads everything and since I am doing the console.log in a different script file which is then loaded from the main.htm file, it executes before everything else.

This is a good use case for the document_complete event handler. Is there a way to pass the frame to the EventHandler?

@thecodrr
Copy link
Author

Okay, didn't work.

No idea why this is resulting in an error.

 // set the global only for this window
    if let Err(e) = frame.set_variable("svdocument", Value::from("hello, document")) {
        eprintln!("frame.set_variable: {:?}", e);
    }

Any way to see the error message?

@pravic
Copy link
Member

pravic commented Feb 22, 2022

Any way to see the error message?

Well, it does print something like "frame.set_variable: INVALID_PARAMETER". Nothing more: https://github.com/c-smile/sciter-js-sdk/blob/main/include/sciter-x-dom.h#L59-L79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants