trying to figure out a way to display this data

This commit is contained in:
Oliver Atkinson 2024-12-13 11:00:51 -07:00
parent 7235326826
commit 635b3a3df7
10 changed files with 116 additions and 5 deletions

BIN
browser/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

5
browser/jsconfig.json Normal file
View File

@ -0,0 +1,5 @@
{
"typeAcquisition": {
"include": ["firefox-webext-browser"]
}
}

14
browser/main/main.html Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Now click "load" in the extension window</h1>
<div id="anchor" hidden></div>
</body>
</html>

14
browser/manifest.json Normal file
View File

@ -0,0 +1,14 @@
{
"manifest_version": 2,
"name": "Viewer",
"version": "0.1",
"description": "Adds a red border to all webpages matching mozilla.org.",
"permissions": [
"activeTab"
],
"browser_action": {
"default_icon": "icon.png",
"default_title": "Click me!",
"default_popup": "popup/popup.html"
}
}

13
browser/popup/inject.js Normal file
View File

@ -0,0 +1,13 @@
let anchor = document.getElementById('anchor');
if (anchor) {
window.fetch(anchor.innerText)
.then(res => res.text())
.then((body) => {
document.body.textContent = "";
document.body.innerHTML = body;
})
}

23
browser/popup/popup.html Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<button id="newtab">Open canvas</button>
<button id="replacer">Load page</button>
<p id="output"></p>
<script src="popup.js"></script>
</body>
<style>
html {
background: darkolivegreen;
color: azure;
}
body {
height: 200px;
width: 200px;
}
</style>
</html>

21
browser/popup/popup.js Normal file
View File

@ -0,0 +1,21 @@
const output = document.getElementById('output');
let page_url = "http://127.0.0.1:4433/s3/style.css";
document.getElementById("newtab").addEventListener('click', async function(e) {
browser.tabs.create({ url: "/main/main.html" }).then((t) => {
if (t.id) {
output.innerText += t.id
}
})
})
document.getElementById("replacer").addEventListener('click', async function(e) {
browser.tabs.executeScript({ code: injection })
browser.tabs.executeScript({ file: "inject.js" });
})
const injection = `
let head = document.getElementById('anchor');
head.innerText = "${page_url}";
`

View File

@ -1,4 +1,4 @@
use rocket::{fs::FileServer, get, routes, State}; use rocket::{fairing::{Fairing, Info, Kind}, fs::FileServer, get, http::Header, routes, Request, Response, State};
use s3::S3; use s3::S3;
use tracing::{info, Level}; use tracing::{info, Level};
use url::Url; use url::Url;
@ -28,18 +28,20 @@ async fn main() {
}; };
let s3 = S3::connect(&config).await.expect("Failed to connect to minio, aborting."); let s3 = S3::connect(&config).await.expect("Failed to connect to minio, aborting.");
let cors = CORS;
let _ = rocket::build() let _ = rocket::build()
.mount("/", FileServer::new("www/")) .mount("/", FileServer::new("www/"))
.mount("/", routes![get_s3_content]) .mount("/", routes![get_s3_content])
.manage(s3) .manage(s3)
.attach(cors)
.launch() .launch()
.await; .await;
info!("Bye"); info!("Bye");
} }
#[get("/<path>")] #[get("/s3/<path>")]
async fn get_s3_content(path: &str, db: &State<S3>) -> String { async fn get_s3_content(path: &str, db: &State<S3>) -> String {
info!(path); info!(path);
// TODO this is just pseudo-code // TODO this is just pseudo-code
@ -50,3 +52,22 @@ async fn get_s3_content(path: &str, db: &State<S3>) -> String {
"Hello world.".to_owned() "Hello world.".to_owned()
} }
// CORS, to allow other sites to request this from their front-end
pub struct CORS;
#[rocket::async_trait]
impl Fairing for CORS {
fn info(&self) -> Info {
Info {
name: "Add CORS headers to responses",
kind: Kind::Response
}
}
async fn on_response<'r>(&self, _request: &'r Request<'_>, response: &mut Response<'r>) {
response.set_header(Header::new("Access-Control-Allow-Origin", "*"));
response.set_header(Header::new("Access-Control-Allow-Methods", "POST, GET, PATCH, OPTIONS"));
response.set_header(Header::new("Access-Control-Allow-Headers", "*"));
response.set_header(Header::new("Access-Control-Allow-Credentials", "true"));
}
}

View File

@ -9,8 +9,8 @@
<link rel="stylesheet" href="style.css"> <link rel="stylesheet" href="style.css">
</head> </head>
<body> <body>
Hello world <h1>Hello 👋</h1>
<script src="main.js" async defer></script> <p>Please install the extension from <a>here</a>!</p>
<div id="frame"></div> <p>This url is only for internal use, you won't get much out of it.</p>
</body> </body>
</html> </html>

View File