diff --git a/src-tauri/src/app/menu.rs b/src-tauri/src/app/menu.rs index bc2391621..da1d212c3 100644 --- a/src-tauri/src/app/menu.rs +++ b/src-tauri/src/app/menu.rs @@ -71,12 +71,20 @@ pub fn init(context: &Context) -> Menu { always_on_top_menu.into(), #[cfg(target_os = "macos")] titlebar_menu.into(), + // fix: Checking if the site connection is secure + // @link: https://github.com/lencx/ChatGPT/issues/17 + CustomMenuItem::new("user_agent".to_string(), "User Agent") + .accelerator("CmdOrCtrl+U") + .into(), CustomMenuItem::new("switch_origin".to_string(), "Switch Origin") .accelerator("CmdOrCtrl+O") .into(), CustomMenuItem::new("inject_script".to_string(), "Inject Script") .accelerator("CmdOrCtrl+J") .into(), + CustomMenuItem::new("clear_conf".to_string(), "Clear Config") + .accelerator("CmdOrCtrl+D") + .into(), MenuItem::Separator.into(), CustomMenuItem::new("awesome".to_string(), "Awesome ChatGPT") .accelerator("CmdOrCtrl+Z") @@ -156,11 +164,10 @@ pub fn menu_handler(event: WindowMenuEvent) { "restart" => tauri::api::process::restart(&app.env()), // Preferences "inject_script" => open(&app, script_path), + "clear_conf" => utils::clear_conf(&app), + "switch_origin" => window::origin_window(&app), + "user_agent" => window::ua_window(&app), "awesome" => open(&app, conf::AWESOME_URL.to_string()), - "switch_origin" => { - window::origin_window(&app); - // app.get_window("origin").unwrap().show(); - } "titlebar" => { let chat_conf = conf::ChatConfJson::get_chat_conf(); ChatConfJson::amend(&serde_json::json!({ "titlebar": !chat_conf.titlebar })).unwrap(); diff --git a/src-tauri/src/app/setup.rs b/src-tauri/src/app/setup.rs index 3418a8fa8..6812ce9f6 100644 --- a/src-tauri/src/app/setup.rs +++ b/src-tauri/src/app/setup.rs @@ -1,8 +1,4 @@ -use crate::{ - app::window, - conf::{ChatConfJson, USER_AGENT}, - utils, -}; +use crate::{app::window, conf::ChatConfJson, utils}; use tauri::{utils::config::WindowUrl, window::WindowBuilder, App, Manager}; pub fn init(app: &mut App) -> std::result::Result<(), Box> { @@ -25,7 +21,7 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box .initialization_script(include_str!("../assets/jspdf.js")) .initialization_script(include_str!("../assets/core.js")) .initialization_script(include_str!("../assets/export.js")) - .user_agent(USER_AGENT) + .user_agent(&chat_conf.ua_pc) .build()?; #[cfg(not(target_os = "macos"))] @@ -41,7 +37,7 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box .initialization_script(include_str!("../assets/jspdf.js")) .initialization_script(include_str!("../assets/core.js")) .initialization_script(include_str!("../assets/export.js")) - .user_agent(USER_AGENT) + .user_agent(&chat_conf.ua_pc) .build()?; Ok(()) diff --git a/src-tauri/src/app/window.rs b/src-tauri/src/app/window.rs index 2fc6af383..b0c5f08fb 100644 --- a/src-tauri/src/app/window.rs +++ b/src-tauri/src/app/window.rs @@ -17,7 +17,7 @@ pub fn mini_window(handle: &tauri::AppHandle) { .initialization_script(include_str!("../assets/jspdf.js")) .initialization_script(include_str!("../assets/core.js")) .initialization_script(include_str!("../assets/export.js")) - .user_agent(conf::PHONE_USER_AGENT) + .user_agent(&chat_conf.ua_phone) .build() .unwrap() .hide() @@ -26,8 +26,6 @@ pub fn mini_window(handle: &tauri::AppHandle) { pub fn origin_window(handle: &tauri::AppHandle) { let chat_conf = conf::ChatConfJson::get_chat_conf(); - // tauri://localhost/origin - // let url = chat_conf.origin; WindowBuilder::new(handle, "origin", WindowUrl::App(chat_conf.origin.into())) .resizable(false) .fullscreen(false) @@ -39,3 +37,17 @@ pub fn origin_window(handle: &tauri::AppHandle) { .build() .unwrap(); } + +pub fn ua_window(handle: &tauri::AppHandle) { + let chat_conf = conf::ChatConfJson::get_chat_conf(); + WindowBuilder::new(handle, "ua", WindowUrl::App(chat_conf.origin.into())) + .resizable(false) + .fullscreen(false) + .inner_size(540.0, 480.0) + .always_on_top(true) + .decorations(false) + .initialization_script(include_str!("../assets/core.js")) + .initialization_script(include_str!("../assets/ua.js")) + .build() + .unwrap(); +} diff --git a/src-tauri/src/assets/origin.js b/src-tauri/src/assets/origin.js index 422008667..2933ffcfe 100644 --- a/src-tauri/src/assets/origin.js +++ b/src-tauri/src/assets/origin.js @@ -54,7 +54,7 @@ function init() { const cancelBtn = document.getElementById('cancel'); const confirmBtn = document.getElementById('confirm'); cancelBtn.addEventListener('click', () => { - window.invoke('form_cancel', { label: 'origin', title: 'Switch Origin', msg: 'Sure you want to give up the switch?' }); + window.invoke('form_cancel', { label: 'origin', title: 'Switch Origin', msg: 'Are you sure you want to cancel editing?' }); }) confirmBtn.addEventListener('click', () => { if (/^https?:\\/\\//.test(input.value)) { diff --git a/src-tauri/src/assets/ua.js b/src-tauri/src/assets/ua.js new file mode 100644 index 000000000..097c5e7fe --- /dev/null +++ b/src-tauri/src/assets/ua.js @@ -0,0 +1,89 @@ +function init() { + document.body.innerHTML = ` +

User Agent

+
+ + +
+
+ + +
+
+ + +
`; + + const srcipt = document.createElement('script'); + srcipt.innerHTML = `const ua_pc = document.getElementById('ua_pc'); + const ua_phone = document.getElementById('ua_phone'); + const cancelBtn = document.getElementById('cancel'); + const confirmBtn = document.getElementById('confirm'); + cancelBtn.addEventListener('click', () => { + window.invoke('form_cancel', { label: 'ua', title: 'User Agent', msg: 'Are you sure you want to cancel editing?' }); + }) + confirmBtn.addEventListener('click', () => { + window.invoke('form_confirm', { data: { ua_pc: ua_pc.value, ua_phone: ua_phone.value } }); + })`; + document.head.appendChild(srcipt); +} + +// run init +if ( + document.readyState === "complete" || + document.readyState === "interactive" +) { + init(); +} else { + document.addEventListener("DOMContentLoaded", init); +} \ No newline at end of file diff --git a/src-tauri/src/conf.rs b/src-tauri/src/conf.rs index 12e5179a1..d2c73e416 100644 --- a/src-tauri/src/conf.rs +++ b/src-tauri/src/conf.rs @@ -7,8 +7,6 @@ use tauri::Theme; #[cfg(target_os = "macos")] use tauri::TitleBarStyle; -pub const USER_AGENT: &str = "5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"; -pub const PHONE_USER_AGENT: &str = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"; pub const ISSUES_URL: &str = "https://github.com/lencx/ChatGPT/issues"; pub const AWESOME_URL: &str = "https://github.com/lencx/ChatGPT/blob/main/AWESOME.md"; pub const DEFAULT_CHAT_CONF: &str = r#"{ @@ -16,7 +14,18 @@ pub const DEFAULT_CHAT_CONF: &str = r#"{ "theme": "Light", "titlebar": true, "default_origin": "https://chat.openai.com", - "origin": "https://chat.openai.com" + "origin": "https://chat.openai.com", + "ua_pc": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1", + "ua_phone": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1" +}"#; +pub const DEFAULT_CHAT_CONF_MAC: &str = r#"{ + "always_on_top": false, + "theme": "Light", + "titlebar": false, + "default_origin": "https://chat.openai.com", + "origin": "https://chat.openai.com", + "ua_pc": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1", + "ua_phone": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1" }"#; pub struct ChatState { @@ -33,11 +42,13 @@ impl ChatState { #[derive(serde::Serialize, serde::Deserialize, Debug, Clone)] pub struct ChatConfJson { + pub titlebar: bool, pub always_on_top: bool, pub theme: String, - pub titlebar: bool, pub default_origin: String, pub origin: String, + pub ua_pc: String, + pub ua_phone: String, } impl ChatConfJson { @@ -49,17 +60,7 @@ impl ChatConfJson { create_file(&conf_file).unwrap(); #[cfg(target_os = "macos")] - fs::write( - &conf_file, - r#"{ - "always_on_top": false, - "theme": "Light", - "titlebar": false, - "default_origin": "https://chat.openai.com", - "origin": "https://chat.openai.com" - }"#, - ) - .unwrap(); + fs::write(&conf_file, DEFAULT_CHAT_CONF_MAC).unwrap(); #[cfg(not(target_os = "macos"))] fs::write(&conf_file, DEFAULT_CHAT_CONF).unwrap(); @@ -72,7 +73,8 @@ impl ChatConfJson { } pub fn get_chat_conf() -> Self { - let config_file = fs::read_to_string(ChatConfJson::conf_path()).unwrap(); + let config_file = fs::read_to_string(ChatConfJson::conf_path()) + .unwrap_or_else(|_| DEFAULT_CHAT_CONF.to_string()); let config: Value = serde_json::from_str(&config_file).expect("failed to parse chat.conf.json"); serde_json::from_value(config).unwrap_or_else(|_| ChatConfJson::chat_conf_default()) diff --git a/src-tauri/src/utils.rs b/src-tauri/src/utils.rs index 58f37fe9a..841ab6265 100644 --- a/src-tauri/src/utils.rs +++ b/src-tauri/src/utils.rs @@ -4,6 +4,7 @@ use std::{ path::{Path, PathBuf}, process::Command, }; +use tauri::Manager; // use tauri::utils::config::Config; pub fn chat_root() -> PathBuf { @@ -61,3 +62,20 @@ pub fn open_file(path: PathBuf) { #[cfg(target_os = "linux")] Command::new("xdg-open").arg(path).spawn().unwrap(); } + +pub fn clear_conf(app: &tauri::AppHandle) { + let root = chat_root(); + let app2 = app.clone(); + let msg = format!("Path: {}\nAre you sure to clear all ChatGPT configurations? Please backup in advance if necessary!", root.to_string_lossy()); + tauri::api::dialog::ask( + app.get_window("core").as_ref(), + "Clear Config", + msg, + move |is_ok| { + if is_ok { + fs::remove_dir_all(root).unwrap(); + tauri::api::process::restart(&app2.env()); + } + }, + ); +}