summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 90d23a2649344484f9c0817827f1c3615934e978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::fmt::{Debug, Display, Formatter};
use hyper::{Client, Uri};
use hyper::body::Buf;
use serde::{ Serialize, Deserialize };
use serde_json;
use tungstenite::Message;

type TokioResult<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

enum AppError {
    ContentNotFound
}

impl Debug for AppError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let message = match self {
            AppError::ContentNotFound => "Content not found"
        };

        write!(f, "{}", message)
    }
}

impl Display for AppError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}

unsafe impl Send for AppError { }
unsafe impl Sync for AppError { }

impl std::error::Error for AppError { }

#[allow(non_snake_case)]
#[allow(dead_code)]
#[derive(Deserialize)]
struct WebContent {
    description: String,
    devtoolsFrontendUrl: String,
    id: String,
    title: String,
    r#type: String,
    url: String,
    webSocketDebuggerUrl: String
}

#[allow(non_snake_case)]
#[allow(dead_code)]
#[derive(Serialize)]
struct DebuggerCommandParams {
    expression: String,
    userGesture: bool
}

#[allow(non_snake_case)]
#[allow(dead_code)]
#[derive(Serialize)]
struct DebuggerCommand {
    id: u32,
    method: String,
    params: DebuggerCommandParams
}

async fn get_web_content(url: Uri) -> TokioResult<Vec<WebContent>> {
    let client = Client::new();
    let response = client.get(url).await?;
    let body = hyper::body::aggregate(response).await?;

    let data = String::from(std::str::from_utf8(body.chunk())?);

    Ok(serde_json::from_str(data.as_str())?)
}

#[tokio::main]
async fn main() -> TokioResult<()> {
    let url = "http://127.0.0.1:8080/json".parse::<hyper::Uri>().unwrap();

    let contents = get_web_content(url).await?;

    println!("Available Content:");
    for content in &contents {
        println!("  {}", content.title);
    }

    let mut quick_access_debug_url: Option<String> = None;
    for content in &contents {
        if content.title == "QuickAccess" {
            quick_access_debug_url = Some(content.webSocketDebuggerUrl.clone());
        }
    }

    if let Some(url) = quick_access_debug_url {

        let (mut socket, _) = tungstenite::connect(url)?;

        let command = DebuggerCommand {
            id: 1,
            method: String::from("Runtime.evaluate"),
            params: DebuggerCommandParams {
                expression: String::from(include_str!("plugin_page.js")),
                userGesture: true
            }
        };

        socket.write_message(Message::Text(serde_json::to_string(&command)?))?;

        let response = socket.read_message()?;

        println!("{}", response);
        
        socket.close(None)?;

    } else {
        return Err(AppError::ContentNotFound.into());
    }


    Ok(())
}