Skip to content

Instantly share code, notes, and snippets.

@BHznJNs
Created October 27, 2025 13:29
Show Gist options
  • Select an option

  • Save BHznJNs/53e58ce735032226c9dbe3e888627309 to your computer and use it in GitHub Desktop.

Select an option

Save BHznJNs/53e58ce735032226c9dbe3e888627309 to your computer and use it in GitHub Desktop.
Tauri plugin that is used to inject variables into the webview global namespace.
use std::collections::HashMap;
use tauri::{
plugin::{Builder, TauriPlugin},
Runtime,
};
fn create_init_script(vars: HashMap<&str, String>) -> String {
let vars_str = vars
.iter()
.map(|(k, v)| format!("{}: '{}'", k, v))
.collect::<Vec<_>>()
.join(",\n");
format!(
r#"
console.log("Successfully injected InputShare variables");
window.__INJECTED__ = {{
{}
}};
"#,
vars_str
)
}
pub fn init<R: Runtime>(vars: HashMap<&str, String>) -> TauriPlugin<R> {
let init_script = create_init_script(vars);
Builder::new("inject_vars")
.js_init_script(init_script)
.build()
}
@BHznJNs
Copy link
Author

BHznJNs commented Oct 27, 2025

Usage in lib.rs:

  let app = tauri::Builder::default()
    .plugin(plugins::inject_vars::init(HashMap::from([
      ("var_name", var_value),
    ])))

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