Kaynağa Gözat

feat(tauri) add `init_script` plugin feature (#863)

Lucas Fernandes Nogueira 5 yıl önce
ebeveyn
işleme
f8081c0e34
2 değiştirilmiş dosya ile 19 ekleme ve 1 silme
  1. 3 1
      tauri/src/app/runner.rs
  2. 16 0
      tauri/src/plugin.rs

+ 3 - 1
tauri/src/app/runner.rs

@@ -275,8 +275,10 @@ fn build_webview(
             window.__TAURI_INVOKE_HANDLER__({{ cmd: "__initialized" }})
           }})
         }}
+        {plugin_init}
       "#,
-      event_init = init()
+      event_init = init(),
+      plugin_init = crate::plugin::init_script()
     ))
     .title(Box::leak(title))
     .width(width as usize)

+ 16 - 0
tauri/src/plugin.rs

@@ -3,6 +3,10 @@ use webview_rust_sys::Webview;
 
 /// The plugin interface.
 pub trait Plugin {
+  /// The JS script to evaluate on init.
+  fn init_script(&self) -> Option<String> {
+    None
+  }
   /// Callback invoked when the webview is created.
   #[allow(unused_variables)]
   fn created(&self, webview: &mut Webview) {}
@@ -37,6 +41,18 @@ fn run_plugin<T: FnMut(&Box<dyn Plugin>)>(mut callback: T) {
   });
 }
 
+pub(crate) fn init_script() -> String {
+  let mut init = String::new();
+
+  run_plugin(|plugin| {
+    if let Some(init_script) = plugin.init_script() {
+      init.push_str(&format!("(function () {{ {} }})();", init_script));
+    }
+  });
+
+  init
+}
+
 pub(crate) fn created(webview: &mut Webview) {
   run_plugin(|ext| {
     ext.created(webview);