From 263c0bc0656671e4554e13f152900b292fcce1c7 Mon Sep 17 00:00:00 2001 From: AmokDev Date: Tue, 18 Mar 2025 14:23:43 +0300 Subject: [PATCH] added rustapp with dsrpc + api, updated frontend --- app/Cargo.toml | 11 +++++++++ app/src/discord/mod.rs | 1 + app/src/discord/rpc.rs | 40 ++++++++++++++++++++++++++++++++ app/src/main.rs | 12 ++++++++++ app/src/network/api.rs | 23 +++++++++++++++++++ app/src/network/mod.rs | 1 + manifest.json | 12 ++++++++-- popup.html | 11 ++++----- popup.js | 52 ++++++++++++++++++++++++++++++++++++++++++ rpc.js | 20 ++++++++++++++++ service-worker.js | 1 + 11 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 app/Cargo.toml create mode 100644 app/src/discord/mod.rs create mode 100644 app/src/discord/rpc.rs create mode 100644 app/src/main.rs create mode 100644 app/src/network/api.rs create mode 100644 app/src/network/mod.rs create mode 100644 popup.js create mode 100644 rpc.js diff --git a/app/Cargo.toml b/app/Cargo.toml new file mode 100644 index 0000000..7116d22 --- /dev/null +++ b/app/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "DiscordRPC-HDRezcord-APP" +version = "0.1.0" +edition = "2021" + +[dependencies] +actix-web = "4" +rust-discord-activity = { git = "https://github.com/AmokDev/rust-discord-activity", version = "0.3.1" } +serde = { version = "1.0", features = ["derive"] } +tokio = "1.44.1" + diff --git a/app/src/discord/mod.rs b/app/src/discord/mod.rs new file mode 100644 index 0000000..25849ae --- /dev/null +++ b/app/src/discord/mod.rs @@ -0,0 +1 @@ +pub mod rpc; \ No newline at end of file diff --git a/app/src/discord/rpc.rs b/app/src/discord/rpc.rs new file mode 100644 index 0000000..ed0edb4 --- /dev/null +++ b/app/src/discord/rpc.rs @@ -0,0 +1,40 @@ +use rust_discord_activity::{ + DiscordClient, + Asset, + Timestamp, + Activity, + ActivityType, + Payload, + EventName, + EventData, +}; +use std::time::{SystemTime, UNIX_EPOCH}; +use tokio::time::Duration; + +pub async fn start_presence() { + let mut client = DiscordClient::new("1351391108088987748"); + + let _ = client.connect(); + + let limg = Some(String::from("https://i.ibb.co/SDDCLdHN/discord-logo-discord-icon-transparent-free-png.png")); + let asset = Asset::new(limg.clone(), Some(String::from("#noWar")), limg, None); + let now_in_millis = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis(); + let timestamps = Timestamp::new(Some(now_in_millis - 10000), Some(now_in_millis + 100000)); + let mut activity = Activity::new(); + + activity + .set_state(Some("Сезон 1 Серия 5".into())) + .set_activity_type(Some(ActivityType::WATCHING)) + .set_details(Some("Элементарно".parse().unwrap())) + .set_timestamps(Some(timestamps)) + .set_assets(Some(asset)) + .set_instance(Some(true)); + + let payload = Payload::new(EventName::Activity, EventData::Activity(activity)); + + let _ = client.send_payload(payload); + println!(":: Rich Presence started!"); + loop { + tokio::time::sleep(Duration::from_secs(5)).await; + } +} \ No newline at end of file diff --git a/app/src/main.rs b/app/src/main.rs new file mode 100644 index 0000000..0612f31 --- /dev/null +++ b/app/src/main.rs @@ -0,0 +1,12 @@ +mod network; +mod discord; + +use network::api::start_api; +use discord::rpc::start_presence; + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + tokio::spawn(start_presence()); + start_api().await?; + Ok(()) +} \ No newline at end of file diff --git a/app/src/network/api.rs b/app/src/network/api.rs new file mode 100644 index 0000000..e46ff54 --- /dev/null +++ b/app/src/network/api.rs @@ -0,0 +1,23 @@ +use actix_web::{get, App, HttpResponse, HttpServer, Responder}; +use serde::Serialize; + +#[derive(Serialize)] +struct MyResponse { + message: String, +} + +#[get("/")] +async fn hello() -> impl Responder { + HttpResponse::Ok().json(MyResponse { message: "Hello, world!".to_string() }) +} + +pub async fn start_api() -> std::io::Result<()> { + println!(":: API Started!"); + HttpServer::new(|| { + App::new() + .service(hello) + }) + .bind("127.0.0.1:2006")? + .run() + .await +} \ No newline at end of file diff --git a/app/src/network/mod.rs b/app/src/network/mod.rs new file mode 100644 index 0000000..c78317c --- /dev/null +++ b/app/src/network/mod.rs @@ -0,0 +1 @@ +pub mod api; \ No newline at end of file diff --git a/manifest.json b/manifest.json index 987313a..8e81e85 100644 --- a/manifest.json +++ b/manifest.json @@ -12,8 +12,16 @@ "action": { "default_popup":"popup.html" }, - "permissions": [], + "permissions": [ + "storage" + ], "background": { "service_worker": "service-worker.js" - } + }, + "web_accessible_resources": [ + { + "resources": ["rpc.js"], + "matches": [""] + } + ] } \ No newline at end of file diff --git a/popup.html b/popup.html index 7e29dcb..bd5f56e 100644 --- a/popup.html +++ b/popup.html @@ -12,17 +12,16 @@

HDRezcord

-
+

+
- +
-
-
- +
-
+ \ No newline at end of file diff --git a/popup.js b/popup.js new file mode 100644 index 0000000..254e9cf --- /dev/null +++ b/popup.js @@ -0,0 +1,52 @@ +const on = document.getElementById("on"); +const off = document.getElementById("off"); +const plugin_status = document.getElementById("status"); + +function edit_status() { + chrome.storage.local.get(['pluginEnabled'], function(result) { + if (result.pluginEnabled === true) { + chrome.storage.local.set({ pluginEnabled: false }); + plugin_status.innerHTML = "Plugin is disabled"; + } else if (result.pluginEnabled === false) { + chrome.storage.local.set({ pluginEnabled: true }); + plugin_status.innerHTML = "Plugin is enabled"; + } + console.log(result.pluginEnabled); + }); +} + +function toggle_status() { + on.classList.toggle("is-outlined"); + off.classList.toggle("is-outlined"); + edit_status(); +} + +function get_status() { + chrome.storage.local.get(['pluginEnabled'], function(result) { + if (result.pluginEnabled === true) { + plugin_status.innerHTML = "Plugin is disabled"; + } else if (result.pluginEnabled === false) { + plugin_status.innerHTML = "Plugin is enabled"; + } + console.log(result.pluginEnabled); + }); +} + +document.addEventListener("DOMContentLoaded", function() { + chrome.storage.local.get(['pluginEnabled'], function(result) { + if (result.pluginEnabled === undefined) { + chrome.storage.local.set({ pluginEnabled: false }); + } else if (result.pluginEnabled === true) { + on.classList.toggle("is-outlined"); + } else if (result.pluginEnabled === false) { + off.classList.toggle("is-outlined"); + } + }); + on.addEventListener("click", function() { + toggle_status(); + }); + off.addEventListener("click", function() { + toggle_status(); + }); + get_status(); +}); \ No newline at end of file diff --git a/rpc.js b/rpc.js new file mode 100644 index 0000000..c7624b9 --- /dev/null +++ b/rpc.js @@ -0,0 +1,20 @@ +const rpc = require('discord-rpc'); +const clientId = '1351391108088987748'; +rpc.register(clientId); + +const client = new rpc.Client({ transport: 'ipc' }); + +client.on('ready', () => { + console.log('RPC запущен!'); + client.setActivity({ + details: 'Играю в вашу игру', + state: 'Нахожусь на уровне 1', + startTimestamp: new Date(), + // largeImageKey: 'image_key', // замените на ключ изображения + // largeImageText: 'Дополнительный текст', + instance: false, + }); +}); + +client.login({ clientId }).catch(console.error); + diff --git a/service-worker.js b/service-worker.js index e69de29..0dc1ef5 100644 --- a/service-worker.js +++ b/service-worker.js @@ -0,0 +1 @@ +importScripts("rpc.js")