This commit is contained in:
lencx 2022-12-07 18:22:18 +08:00
parent 2f9f201c8b
commit 386a58a499
34 changed files with 4177 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.DS_Store
node_modules/
target/

3
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"recommendations": ["tauri-apps.tauri-vscode", "rust-lang.rust-analyzer"]
}

0
dist/.gitkeep vendored Normal file
View File

BIN
logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

14
package.json Normal file
View File

@ -0,0 +1,14 @@
{
"name": "chatgpt",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "yarn tauri dev",
"build": "yarn tauri build",
"tauri": "tauri"
},
"devDependencies": {
"@tauri-apps/cli": "^1.2.1"
}
}

3682
src-tauri/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

28
src-tauri/Cargo.toml Normal file
View File

@ -0,0 +1,28 @@
[package]
name = "chatgpt"
version = "0.0.0"
authors = ["lencx <cxin1314@gmail.com>"]
description = "ChatGPT Desktop Application"
repository = "https://github.com/lencx/ChatGPT"
license = "MIT"
edition = "2021"
rust-version = "1.57"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies]
tauri-build = {version = "1.2", features = [] }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = {version = "1.2", features = ["api-all", "system-tray"] }
anyhow = "1.0.66"
[features]
# by default Tauri runs in production mode
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL
default = [ "custom-protocol" ]
# this feature is used used for production builds where `devPath` points to the filesystem
# DO NOT remove this
custom-protocol = [ "tauri/custom-protocol" ]

3
src-tauri/build.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
tauri_build::build()
}

BIN
src-tauri/icons/128x128.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
src-tauri/icons/32x32.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

BIN
src-tauri/icons/icon.icns Normal file

Binary file not shown.

BIN
src-tauri/icons/icon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

BIN
src-tauri/icons/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

31
src-tauri/src/core.js Normal file
View File

@ -0,0 +1,31 @@
// *** Core Script ***
document.addEventListener('DOMContentLoaded', async () => {
const topStyleDom = document.createElement("style");
topStyleDom.innerHTML = `#chatgpt-app-window-top{position:fixed;top:0;z-index:999999999;width:100%;height:24px;background:transparent;cursor:grab;cursor:-webkit-grab;user-select:none;-webkit-user-select:none;}#chatgpt-app-window-top:active {cursor:grabbing;cursor:-webkit-grabbing;}`;
document.head.appendChild(topStyleDom);
const topDom = document.createElement("div");
topDom.id = "chatgpt-app-window-top";
document.body.appendChild(topDom);
topDom.addEventListener("mousedown", () => __TAURI_INVOKE__("drag_window"));
topDom.addEventListener("touchstart", () => __TAURI_INVOKE__("drag_window"));
topDom.addEventListener("dblclick", () => __TAURI_INVOKE__("fullscreen"));
document.addEventListener("click", (e) => {
const origin = e.target.closest("a");
if (origin && origin.href && origin.target !== '_self') {
origin.target = "_self";
}
});
document.addEventListener('wheel', function(event) {
const deltaX = event.wheelDeltaX;
if (Math.abs(deltaX) >= 50) {
if (deltaX > 0) {
window.history.go(-1);
} else {
window.history.go(1);
}
}
});
})

27
src-tauri/src/main.rs Normal file
View File

@ -0,0 +1,27 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
mod utils;
mod wa;
use tauri::SystemTray;
use wa::{cmd, menu, setup};
fn main() {
let context = tauri::generate_context!();
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
cmd::drag_window,
cmd::fullscreen,
])
.setup(setup::init)
.menu(menu::init(&context))
.system_tray(SystemTray::new())
.on_menu_event(menu::menu_handler)
.on_system_tray_event(menu::tray_handler)
.run(context)
.expect("error while running ChatGPT application");
}

53
src-tauri/src/utils.rs Normal file
View File

@ -0,0 +1,53 @@
use anyhow::Result;
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use tauri::utils::config::Config;
pub fn get_tauri_conf() -> Option<Config> {
let config_file = include_str!("../tauri.conf.json");
let config: Config =
serde_json::from_str(config_file).expect("failed to parse tauri.conf.json");
Some(config)
}
#[derive(serde::Serialize, serde::Deserialize, Debug)]
pub struct WaJson {
pub url: String,
pub resizable: bool,
pub theme: tauri::Theme,
pub mode: String,
pub title: String,
pub always_on_top: bool,
pub hidden_title: bool,
pub hide_title_bar: bool,
}
pub fn exists(path: &Path) -> bool {
Path::new(path).exists()
}
pub fn create_file(path: &Path) -> Result<File> {
if let Some(p) = path.parent() {
fs::create_dir_all(p)?
}
File::create(path).map_err(Into::into)
}
pub fn script_path() -> PathBuf {
let root = tauri::api::path::home_dir().unwrap().join(".chatgpt");
let script_file = root.join("main.js");
if !exists(&script_file) {
create_file(&script_file).unwrap();
fs::write(&script_file, format!("// *** ChatGPT User Script ***\n// @github: https://github.com/lencx/ChatGPT \n// @path: {}\n\nconsole.log('🤩 Hello ChatGPT!!!');", &script_file.to_string_lossy())).unwrap();
}
script_file
}
pub fn user_script() -> String {
let user_script_content = fs::read_to_string(script_path()).unwrap_or_else(|_| "".to_string());
format!(
"window.addEventListener('DOMContentLoaded', function() {{\n{}\n}})",
user_script_content
)
}

16
src-tauri/src/wa/cmd.rs Normal file
View File

@ -0,0 +1,16 @@
use tauri::Manager;
#[tauri::command]
pub fn drag_window(app: tauri::AppHandle) {
app.get_window("core").unwrap().start_dragging().unwrap();
}
#[tauri::command]
pub fn fullscreen(app: tauri::AppHandle) {
let win = app.get_window("core").unwrap();
if win.is_fullscreen().unwrap() {
win.set_fullscreen(false).unwrap();
} else {
win.set_fullscreen(true).unwrap();
}
}

160
src-tauri/src/wa/menu.rs Normal file
View File

@ -0,0 +1,160 @@
use tauri::{
utils::assets::EmbeddedAssets, AboutMetadata, AppHandle, Context, CustomMenuItem, Manager,
Menu, MenuItem, Submenu, SystemTrayEvent, WindowMenuEvent,
};
use crate::utils;
// --- Menu
pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
let name = &context.package_info().name;
let app_menu = Submenu::new(
name,
Menu::new()
.add_native_item(MenuItem::About(name.into(), AboutMetadata::default()))
.add_native_item(MenuItem::Separator)
.add_item(
CustomMenuItem::new("inject_script".to_string(), "Inject Script")
.accelerator("CmdOrCtrl+J"),
)
.add_native_item(MenuItem::Separator)
.add_native_item(MenuItem::Hide)
.add_native_item(MenuItem::HideOthers)
.add_native_item(MenuItem::ShowAll)
.add_native_item(MenuItem::Separator)
.add_native_item(MenuItem::Quit),
);
let edit_menu = Submenu::new(
"Edit",
Menu::new()
.add_native_item(MenuItem::Undo)
.add_native_item(MenuItem::Redo)
.add_native_item(MenuItem::Separator)
.add_native_item(MenuItem::Cut)
.add_native_item(MenuItem::Copy)
.add_native_item(MenuItem::Paste)
.add_native_item(MenuItem::SelectAll),
);
let view_menu = Submenu::new(
"View",
Menu::new()
.add_item(
CustomMenuItem::new("go_back".to_string(), "Go Back").accelerator("CmdOrCtrl+Left"),
)
.add_item(
CustomMenuItem::new("go_forward".to_string(), "Go Forward")
.accelerator("CmdOrCtrl+Right"),
)
.add_item(
CustomMenuItem::new("scroll_top".to_string(), "Scroll to Top of Screen")
.accelerator("CmdOrCtrl+Up"),
)
.add_item(
CustomMenuItem::new("scroll_bottom".to_string(), "Scroll to Bottom of Screen")
.accelerator("CmdOrCtrl+Down"),
)
.add_native_item(MenuItem::Separator)
.add_item(
CustomMenuItem::new("reload".to_string(), "Refresh the Screen")
.accelerator("CmdOrCtrl+R"),
),
);
let help_menu = Submenu::new(
"Help",
Menu::new()
.add_item(CustomMenuItem::new("report_bug".to_string(), "Report Bug"))
.add_item(
CustomMenuItem::new("dev_tools".to_string(), "Toggle Developer Tools")
.accelerator("CmdOrCtrl+Shift+I"),
),
);
Menu::new()
.add_submenu(app_menu)
.add_submenu(edit_menu)
.add_submenu(view_menu)
.add_submenu(help_menu)
}
// --- Menu Event
pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
let win = Some(event.window()).unwrap();
let app = win.app_handle();
match event.menu_item_id() {
// App
"inject_script" => {
tauri::api::shell::open(
&app.shell_scope(),
utils::script_path().to_string_lossy(),
None,
)
.unwrap();
}
// View
"go_back" => {
win.eval("window.history.go(-1)").unwrap();
}
"go_forward" => {
win.eval("window.history.go(1)").unwrap();
}
"scroll_top" => {
win.eval(
r#"window.scroll({
top: 0,
left: 0,
behavior: "smooth"
})"#,
)
.unwrap();
}
"scroll_bottom" => {
win.eval(
r#"window.scroll({
top: document.body.scrollHeight,
left: 0,
behavior: "smooth",
})"#,
)
.unwrap();
}
"reload" => {
win.eval("window.location.reload()").unwrap();
}
// Help
"report_bug" => {
tauri::api::shell::open(
&app.shell_scope(),
"https://github.com/lencx/ChatGPT/issues",
None,
)
.unwrap();
}
"dev_tools" => {
win.open_devtools();
win.close_devtools();
}
_ => (),
}
}
// --- SystemTray Event
pub fn tray_handler(app: &AppHandle, event: SystemTrayEvent) {
if let SystemTrayEvent::LeftClick {
position: _,
size: _,
..
} = event
{
let win = app.get_window("core").unwrap();
if win.is_visible().unwrap() {
win.hide().unwrap();
} else {
win.show().unwrap();
win.set_focus().unwrap();
}
}
}

3
src-tauri/src/wa/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod cmd;
pub mod menu;
pub mod setup;

21
src-tauri/src/wa/setup.rs Normal file
View File

@ -0,0 +1,21 @@
use tauri::{utils::config::WindowUrl, window::WindowBuilder, App, TitleBarStyle};
use crate::utils;
pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>> {
let conf = utils::get_tauri_conf().unwrap();
let url = conf.build.dev_path.to_string();
WindowBuilder::new(app, "core", WindowUrl::App(url.into()))
.resizable(true)
.fullscreen(false)
.initialization_script(include_str!("../core.js"))
.initialization_script(&utils::user_script())
.title_bar_style(TitleBarStyle::Overlay)
.inner_size(800.0, 600.0)
.hidden_title(true)
.user_agent("5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
.build()?;
Ok(())
}

59
src-tauri/tauri.conf.json Normal file
View File

@ -0,0 +1,59 @@
{
"build": {
"beforeDevCommand": "",
"beforeBuildCommand": "",
"devPath": "https://chat.openai.com",
"distDir": "../dist"
},
"package": {
"productName": "ChatGPT",
"version": "../package.json"
},
"tauri": {
"allowlist": {
"all": true
},
"systemTray": {
"iconPath": "icons/icon.png",
"iconAsTemplate": false
},
"bundle": {
"active": true,
"category": "WA",
"copyright": "",
"deb": {
"depends": []
},
"externalBin": [],
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
],
"identifier": "com.lencx.wa",
"longDescription": "Web APP",
"macOS": {
"entitlements": null,
"exceptionDomain": "",
"frameworks": [],
"providerShortName": null,
"signingIdentity": null
},
"shortDescription": "WA",
"targets": "all",
"windows": {
"certificateThumbprint": null,
"digestAlgorithm": "sha256",
"timestampUrl": ""
}
},
"security": {
"csp": null
},
"updater": {
"active": false
}
}
}

10
src-tauri/wa.json Normal file
View File

@ -0,0 +1,10 @@
{
"url": "https://weread.qq.com",
"resizable": true,
"theme": "Light",
"mode": "PC",
"title": "WA",
"always_on_top": false,
"hidden_title": true,
"hide_title_bar": true
}

63
yarn.lock Normal file
View File

@ -0,0 +1,63 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@tauri-apps/cli-darwin-arm64@1.2.1":
version "1.2.1"
resolved "https://registry.npmmirror.com/@tauri-apps/cli-darwin-arm64/-/cli-darwin-arm64-1.2.1.tgz#e75d6582f76b6ef33f251f9b559e7692b7892faa"
integrity sha512-WiAxSON52owFI65Whd3NjcKjXdO1zJfafBVZ3v+Y3F2zlXhRPiirXbtefAc3mvN56zDz/pfi018Qb4XesuVzHA==
"@tauri-apps/cli-darwin-x64@1.2.1":
version "1.2.1"
resolved "https://registry.npmmirror.com/@tauri-apps/cli-darwin-x64/-/cli-darwin-x64-1.2.1.tgz#3fefdb0154d7d804ea1b08fad342abec625b36e4"
integrity sha512-jfumnrn7RYKVtDmHgrQhImoxpqT51bDrO4KxgpIXaYSaf6MdG2JT72dwUovPrURw0JX2Z/Elihq+dYbrsKoV/w==
"@tauri-apps/cli-linux-arm-gnueabihf@1.2.1":
version "1.2.1"
resolved "https://registry.npmmirror.com/@tauri-apps/cli-linux-arm-gnueabihf/-/cli-linux-arm-gnueabihf-1.2.1.tgz#6f5f403334b840d0085fec38cca4551c5c6b36a6"
integrity sha512-n4p6Ekn6Wa9X/klUGevEGgoWAGApGGsLrJYE4c8bKTbAUfQ9Nyzjh8gK/GDii1dg9oRW0FdXDa6BJa7aEEj9sA==
"@tauri-apps/cli-linux-arm64-gnu@1.2.1":
version "1.2.1"
resolved "https://registry.npmmirror.com/@tauri-apps/cli-linux-arm64-gnu/-/cli-linux-arm64-gnu-1.2.1.tgz#d1c536d0fe4819029de9132974bdfb6fa08e55a7"
integrity sha512-oAHkQQGfNCh8pQQHuDzzqt+S5sOj3tiUkySaquR2z/AQEHeDGAMrRGLZwOiDw9Xvu7qxFiF9H0e5OMK7BkncDw==
"@tauri-apps/cli-linux-arm64-musl@1.2.1":
version "1.2.1"
resolved "https://registry.npmmirror.com/@tauri-apps/cli-linux-arm64-musl/-/cli-linux-arm64-musl-1.2.1.tgz#22dd054d3ae2a923d5f7d3c7f529f48e7113ec19"
integrity sha512-1dEZ5fdFYkMyB1U7ZkDJUlwYwCeqy9Y3vXmtZ6pCxrvgs844s8+RIFDuMU42pTN+lUxfFeQARmv0LpS4eF/QWQ==
"@tauri-apps/cli-linux-x64-gnu@1.2.1":
version "1.2.1"
resolved "https://registry.npmmirror.com/@tauri-apps/cli-linux-x64-gnu/-/cli-linux-x64-gnu-1.2.1.tgz#bfe23577a46a74699b27714d82162f0ae9280090"
integrity sha512-wkgCD3s5P6tgBPnn0/gDx7MXMx2Hx+jdA+JP8zdCq4cIeYXlxdZG/zXbHb5ldPadNh582lzHiGg+Pmc+wDg2fA==
"@tauri-apps/cli-linux-x64-musl@1.2.1":
version "1.2.1"
resolved "https://registry.npmmirror.com/@tauri-apps/cli-linux-x64-musl/-/cli-linux-x64-musl-1.2.1.tgz#dce7fb0254a1975238bdde5882115b38271ec9b7"
integrity sha512-zD+a+5cSlYVU0ECOojWp71ok/9jE0DJufzb9oky17XIKV/oiOAG60z4OYRe+oqxYS1TcBt+pUa1/2zlu/6SRdA==
"@tauri-apps/cli-win32-ia32-msvc@1.2.1":
version "1.2.1"
resolved "https://registry.npmmirror.com/@tauri-apps/cli-win32-ia32-msvc/-/cli-win32-ia32-msvc-1.2.1.tgz#b6cb355f2d85704710c845800a29784536fb7240"
integrity sha512-nEgdRd8czaKL1RQfj946dsfzlk6atmD95Fm7NVTVOe77PFHTS3ztHeWK7X6jirCaOF3h/F7qpJVCU6JMnq2tfA==
"@tauri-apps/cli-win32-x64-msvc@1.2.1":
version "1.2.1"
resolved "https://registry.npmmirror.com/@tauri-apps/cli-win32-x64-msvc/-/cli-win32-x64-msvc-1.2.1.tgz#b2b431ffed68fab9024d59ac2a2bafb02c5b9a5a"
integrity sha512-ceWiQkmNNRrnfgbLMnndT4QF1IyLK7aOJsMqS/HueshAwmVQWnC1DUE0C58Taetgq38Cavc4gGnZOCHuH6ZuTw==
"@tauri-apps/cli@^1.2.1":
version "1.2.1"
resolved "https://registry.npmmirror.com/@tauri-apps/cli/-/cli-1.2.1.tgz#d7f61547915a8b052a92197ac70b8322838c6d2d"
integrity sha512-JrFFT1/4V+AGSRjKdgszZwOr+/a1519LDhNxBkbsIg6D/kFK+3kk1qImaQBY9DvvIWK6IV4whc8OAkgxm+Sl3w==
optionalDependencies:
"@tauri-apps/cli-darwin-arm64" "1.2.1"
"@tauri-apps/cli-darwin-x64" "1.2.1"
"@tauri-apps/cli-linux-arm-gnueabihf" "1.2.1"
"@tauri-apps/cli-linux-arm64-gnu" "1.2.1"
"@tauri-apps/cli-linux-arm64-musl" "1.2.1"
"@tauri-apps/cli-linux-x64-gnu" "1.2.1"
"@tauri-apps/cli-linux-x64-musl" "1.2.1"
"@tauri-apps/cli-win32-ia32-msvc" "1.2.1"
"@tauri-apps/cli-win32-x64-msvc" "1.2.1"