<div align="center">
# Axum JS Advice Middleware
[<img alt="crates.io" src="https://img.shields.io/crates/v/axum_js_advice.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/axum_js_advice)
[<img alt="github" src="https://img.shields.io/badge/gitlab-mateolafalce/axum_js_advice-8da0cb?style=for-the-badge&labelColor=555555&logo=gitlab" height="20">](https://gitlab.torproject.org/mateolafalce/axum_js_advice)
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-axum_js_advice-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/axum_js_advice)
</div>
This project allows you to includes a custom middleware that injects a JavaScript alert in your axum application, advising users to disable JavaScript for enhanced security.
## Features
- **Custom Middleware**: Adds an HTML `<script>` tag to responses, displaying a warning pop-up if JavaScript is enabled.
- **Request and Response Buffering**: Reads and modifies HTTP request/response bodies to include the custom script.
- **Minimal Setup**: Built with simplicity in mind using `axum` and `tokio`.
## How It Works
The middleware intercepts responses and appends a JavaScript alert to the body. The alert advises users to disable JavaScript for their safety.
### Example Warning Script Injected:
```html
<script>
alert("Warning!\nYou have JavaScript enabled, you are putting yourself at risk!\nPlease disable it immediately!");
</script>
```
### Usage
```bash
cargo add axum_js_advice
```
Then, add axum_js_advice() in your middleware layer, for example:
```rust
use axum::{middleware, Router};
use axum_js_advice::js_advice;
#[tokio::main]
async fn main() {
let app = Router::new()
.route(
"/",
axum::routing::get(|| async move { axum::response::Html("Hello from `POST /`") }),
)
//.layer(middleware::from_fn(OTHER_MIDDLEWARE_RULE))
.layer(middleware::from_fn(js_advice));
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
println!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}
```
## Example Output
```bash
cargo run --example middleware
```
When visiting `http://127.0.0.1:3000/`, the response will include:
```html
<script>
alert("Warning!\nYou have JavaScript enabled, you are putting yourself at risk!\nPlease disable it immediately!");
</script>
Hello from `POST /`
```
If you have JavaScript enabled, a reminder will pop up asking you to disable it; otherwise, you can browse freely.
## License
This project is licensed under the GPL License. See the [LICENSE](./LICENSE) file for details.