Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gtrabanco/elysia-inject-html


https://github.com/gtrabanco/elysia-inject-html

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

        

# Description

This is a plugin for elysia that injects html/js code in the response page.

# Installation

```bash
bun add --exact @gtrabanco/elysia-inject-html
```

# Usage

```js
import { Elysia } from 'elysia';
import { injectHtml } from '@gtrabanco/elysia-inject-html';

const app = new Elysia()
.use(injectCode({
selector: 'body'
code: 'alert("Hello World!")'
}))
.get('/index.html', () =>
new Response('', {
headers: {
'content-type': 'text/html'
}
}))
.listen(8080);
```

You can provide multiple code with the same selector by providing an array of strings:

```js
const app = new Elysia()
.use(injectHtml({
selector: 'body'
code: [
'alert("Hello World!")',
'alert("Hello World 2!")'
]
}))
.get('/index.html', () =>
new Response('', {
headers: {
'content-type': 'text/html'
}
}))
.listen(8080);
```

## Known limitations

Currently it is not working if you create two similar plugins or two plugins that call `afterHandle` because it will fail. This is confirmed until Elysia 0.7.9.