BlueGit

ict rusty-teams

Files

rusty-teams / crates / chatsvc / examples / bundle_tmp.rs

bundle_tmp.rs

f04cbc7f History
bundle_tmp.rs 1.4 KiB Download Raw
1use serde_json::json;
2use session::{App, DEFAULT_ENDPOINT, Scope, Session};
3
4#[tokio::main]
5async fn main() {
6 let endpoint = std::env::var("CDP_ENDPOINT").unwrap_or_else(|_| DEFAULT_ENDPOINT.to_owned());
7 let out = std::env::args().nth(1).expect("out dir");
8 let pattern = std::env::args().nth(2).expect("pattern");
9 let session = Session::connect(&endpoint).await.expect("browser");
10 let body = r#"
11const found = {};
12for (const name of await caches.keys()) {
13 const cache = await caches.open(name);
14 for (const request of await cache.keys()) {
15 if (!new RegExp(args.pattern).test(request.url)) continue;
16 const response = await cache.match(request);
17 found[request.url] = await response.text();
18 }
19}
20const scripts = performance.getEntriesByType('resource').map(entry => entry.name).filter(url => new RegExp(args.pattern).test(url));
21for (const url of scripts) { if (!found[url]) { try { found[url] = await (await fetch(url)).text(); } catch (error) {} } }
22return found;
23"#;
24 let scope = Scope::new("https://substrate.office.com", "SubstrateSearch-Internal.ReadWrite");
25 let found = session.run_with_token(App::Teams, &scope, body, &json!({"pattern": pattern}), false).await.expect("eval");
26 for (url, text) in found.as_object().unwrap() {
27 let name = url.rsplit('/').next().unwrap();
28 std::fs::write(format!("{out}/{name}"), text.as_str().unwrap()).unwrap();
29 println!("{url} {}", text.as_str().unwrap().len());
30 }
31}