#I can't for the life of me get window.eval to work in my app

24 messages · Page 1 of 1 (latest)

misty merlin
#

In my job we're developing a desktop variant of our webapp via tauri and for the past week I cannot get tauri to evaluate JS code.

I've looked through most of the github issues and none answer my questions about what I'm doing wrong.

This here is our lib.rs file or atleast a part of it, I cannot share everything. How and where should I call the window.eval from? The examples from the documentation dont work for me...


pub fn run() {
    setup_logger();
    info!("Starting Tauri Application Builder...");
    tauri::Builder::default()
        // This plugin MUST be initialized first
        .plugin(tauri_plugin_single_instance::init(|_app, argv, _cwd| {
            info!("Secondary instance triggered. Args: {:?}", argv);
        }))
        .setup(|app| {
            // Handle file association
            let files = parse_files_from_command_line();
            handle_file_associations(app.handle().clone(), files);

            info!("Setup complete. Returning Ok(()).");
            Ok(())
        })
        .plugin(tauri_plugin_oauth::init())
        .plugin(tauri_plugin_deep_link::init())
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
    info!("Application exited from the run loop.");
}

serene holly
#

You haven't created any window at this point, so there's nowhere to call eval. But if you create your window in the tauri.conf.json file, you should first use your AppHandle to query it by name

misty merlin
#

Could you please tell me what do I need to initialize and update before I can use the example from the documentation?

serene holly
#

are you creating the window in the tauri.conf.json?

misty merlin
#

if you mean this

  "app": {
    "windows": [
      {
        "title": "company-name",
        "resizable": true,
        "fullscreen": true
      }
    ],
    "security": {
      "csp": null
    }
  },

``` then yes
serene holly
#

you'll need to update the setup callback:

.setup(|app| {
  // ...some other code
  if let Some(window) = app.get_webview_window("my-window") {
    window.eval("some code")?;
  }

  Ok(())
})
#

Also, add a label field to the window config in the tauri.conf.json. You'll use that label to query the window.

#

You'll need to import the Manager trait too

misty merlin
#

In the snippet i posted manager was imported, on of the evals I've tried was this inside run()

pub fn run() {
    // Note: To see the console with logger messages it might be necessary to run it via the command line
    setup_logger();
    info!("Starting Tauri Application Builder...");
    tauri::Builder::default()
        // This plugin MUST be initialized first
        .plugin(tauri_plugin_single_instance::init(|_app, argv, _cwd| {
            info!("Secondary instance triggered. Args: {:?}", argv);
            // This is executed when another instance is launched.
            // the link in `argv` (argv should contain the deeplink which triggered this)
        }))
        .setup(|app| {

            if let Some(window) = app.get_webview_window("company-name") {
                window.eval("window.eval("document.querySelectorAll('style, link[rel=\"stylesheet\"]').forEach(e => e.remove()");
            }

            // Handle file association
            let files = parse_files_from_command_line();
            handle_file_associations(app.handle().clone(), files);

            info!("Setup complete. Returning Ok(()).");
            Ok(())
        })
        .plugin(tauri_plugin_oauth::init())
        .plugin(tauri_plugin_deep_link::init())
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
    info!("Application exited from the run loop.");
}
#

I wanted some drastic change however nothing happened

warm whale
#

one thing is that get_webview_window's label does not exist in tauri.conf.json (title != label) but that may just got lost in the discord copy

#

but i guess that's a good thing to try as well. log something from inside the if let Some to see if that's good

#

if that's good then in theory the rest of your code snippet can be correct but it can depend on your frontend stack (mainly timing wise). one thing to try if not done yet is to move the eval into on_page_loaded or on_navigation instead as setup may simply be too early (depending on what your eval-ing and what your window is doing)

#

for example in one of my apps i used to set some var on the js window var which worked fine in the setup hook since it didn't depend on the frontend execution time (except that it's not allowed to reload or navigate to keep the var)

misty merlin
#

So It's not my lib.rs which is wrong but the asynchronous frontend ?

warm whale
#

that's possible at least

#

try some console.log in the eval for example to check if it runs at all (on top of the if let Some printing i mentioned above)

misty merlin
#
        .setup(|app| {
            if let Some(window) = app.get_webview_window("main") {
                window.eval("console.log('Hello');");
            }

.....

This with the correct label inside json config also fails to print anything..

warm whale
nocturne rivet
#

yeah i like to use it to start the app in different pages in the same frontend

#

works really well