mirror of
https://github.com/linsa-io/linsa.git
synced 2026-03-31 22:23:28 +02:00
feat: add OSA script for getting current browser url
This commit is contained in:
50
app/src/browser.rs
Normal file
50
app/src/browser.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use osakit::declare_script;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
declare_script! {
|
||||
#[language(JavaScript)]
|
||||
#[source(r#"
|
||||
function safari(name, index) {
|
||||
return Application(name).windows[index].currentTab.url();
|
||||
}
|
||||
|
||||
function chrome(name, index) {
|
||||
return Application(name).windows[index].activeTab().url();
|
||||
}
|
||||
"#)]
|
||||
pub Browser {
|
||||
pub fn safari(name: &str, index: usize) -> String;
|
||||
pub fn chrome(name: &str, index: usize) -> String;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Kind {
|
||||
Safari,
|
||||
Chrome,
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("unable to run JXA function")]
|
||||
JxaRunError(#[from] osakit::ScriptFunctionRunError),
|
||||
}
|
||||
|
||||
impl serde::Serialize for Error {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::ser::Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.to_string().as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_current_browser_url(kind: Kind, name: &str, window: usize) -> Result<String, Error> {
|
||||
let browser = Browser::new().expect("successful script compilation");
|
||||
Ok(match kind {
|
||||
Kind::Safari => browser.safari(name, window)?,
|
||||
Kind::Chrome => browser.chrome(name, window)?,
|
||||
})
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
mod browser;
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.invoke_handler(tauri::generate_handler![browser::get_current_browser_url])
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
Reference in New Issue
Block a user