-
Notifications
You must be signed in to change notification settings - Fork 14
feat: added browser and profile functions #19
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
Changes from all commits
02f61c6
3872a46
8f1579e
950ac46
065e885
5a86218
fb63e25
bee10bf
be2a77c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| use crowser::{browser}; | ||
| use std::{fs, path::PathBuf, io::{self, Read}}; | ||
| use dirs::{config_dir, data_local_dir}; | ||
| use serde_json::Value; | ||
|
|
||
| pub enum Browsers { | ||
| Chrome, | ||
| Edge, | ||
| Brave, | ||
| FireFox, | ||
| Safari | ||
| } | ||
|
|
||
| pub fn get_browsers() -> Vec<String> { | ||
| let browser_vector = browser::get_all_existing_browsers(); | ||
| let browser_names: Vec<String> = browser_vector.iter().map(|s| s.name.to_owned()).collect(); | ||
|
|
||
| return browser_names; | ||
| } | ||
|
|
||
| pub fn get_chrome_based_profiles(os_paths: Vec<&str>) -> Result<Vec<String>, Box<dyn std::error::Error>> { | ||
|
|
||
| let base_dir = if cfg!(target_os = "windows") || cfg!(target_os = "macos") { | ||
| data_local_dir() | ||
| } else { | ||
| config_dir() | ||
| }; | ||
|
|
||
| if let Some(mut path) = base_dir { | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| path.push(os_paths[0]); | ||
|
|
||
| #[cfg(target_os = "macos")] | ||
| path.push(os_paths[1]); | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| path.push(os_paths[2]); | ||
|
|
||
| if path.exists() { | ||
|
|
||
| let mut file = fs::File::open(path)?; | ||
| let mut contents = String::new(); | ||
| file.read_to_string(&mut contents)?; | ||
|
|
||
| let json_value: Value = serde_json::from_str(&contents)?; | ||
|
|
||
| let info_cache = json_value | ||
| .get("profile") | ||
| .and_then(|p| p.get("info_cache")) | ||
| .and_then(|ic| ic.as_object()) | ||
| .ok_or_else(|| {Box::new(io::Error::new(io::ErrorKind::InvalidData, "Could not find 'profile' or 'info_cache' in JSON.")) as Box<dyn std::error::Error>})?; | ||
|
|
||
| let mut profile_names: Vec<String> = Vec::new(); | ||
|
|
||
| for (_profile_key, profile_data) in info_cache.iter() { | ||
| if let Some(name_value) = profile_data.get("gaia_name") { | ||
| if let Some(name_str) = name_value.as_str() { | ||
| profile_names.push(name_str.to_owned()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return Ok(profile_names); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| Ok(Vec::new()) | ||
| } | ||
|
|
||
| pub fn get_chrome_profiles(kind: Browsers) -> Result<Vec<String>, Box<dyn std::error::Error>> { | ||
|
|
||
| let paths: Vec<&str> = match kind { | ||
| Browsers::Chrome => vec![ | ||
| "Google\\Chrome\\User Data\\Local State", | ||
| "Google/Chrome/Local State", | ||
| "google-chrome/Local State", | ||
| ], | ||
| Browsers::Edge => vec![ | ||
| "Microsoft\\Edge\\User Data\\Local State", | ||
| "Microsoft/Edge/Local State", | ||
| "microsoft-edge/Local State", | ||
| ], | ||
| Browsers::Brave => vec![ | ||
| "BraveSoftware\\Brave-Browser\\User Data\\Local State", | ||
| "BraveSoftware/Brave-Browser/Local State", | ||
| "brave/Local State", | ||
| ], | ||
| _ => return Ok(Vec::new()), | ||
| }; | ||
|
|
||
| return get_chrome_based_profiles(paths); | ||
| } | ||
|
|
||
| pub fn get_firefox_profiles() -> Result<Vec<String>, Box<dyn std::error::Error>> { | ||
|
|
||
| let base_dir = if cfg!(target_os = "windows") || cfg!(target_os = "macos") { | ||
| data_local_dir() | ||
| } else { | ||
| dirs::home_dir() | ||
|
Comment on lines
+98
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On Windows the function builds the base directory using Useful? React with 👍 / 👎.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AdityaVKochar can you verify this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. works for both local and roaming |
||
| }; | ||
|
|
||
| if let Some(mut path) = base_dir { | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| path.push("Mozilla\\Firefox\\Profiles"); | ||
|
|
||
| #[cfg(target_os = "macos")] | ||
| path.push("Firefox/Profiles"); | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| path.push("~/.mozilla/firefox"); | ||
|
|
||
| if path.exists() { | ||
| match fs::read_dir(path) { | ||
| Ok(entries) => { | ||
| let profile_names: Vec<String> = entries | ||
| .filter_map(Result::ok) | ||
| .filter_map(|entry| { | ||
| match entry.file_type() { | ||
| Ok(file_type) if file_type.is_dir() => { | ||
| Some(entry.file_name().to_string_lossy().into_owned()) | ||
| } | ||
| _ => None, | ||
| } | ||
| }) | ||
| .collect(); | ||
| return Ok(profile_names); | ||
| } | ||
| Err(e) => { | ||
| eprintln!("Error reading directory: {}", e); | ||
| return Ok(Vec::new()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return Ok(Vec::new()); | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #[tauri::command] | ||
| pub fn get_available_browser() { | ||
| get_browsers(); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.