fix(examples): Use BgUtils to generate pot [skip ci]

+ Improve readme.
This commit is contained in:
Luan
2024-08-26 18:42:14 -03:00
parent 367a6f7ec5
commit d89909a19a
5 changed files with 386 additions and 103 deletions

View File

@@ -1,61 +1,19 @@
# Browser Usage Example
YouTube.js works in the browser!
## Usage
# Browser Usage
To use YouTube.js in the browser you must proxy requests through your own server. You can see our simple reference implementation in Deno in `examples/browser/proxy/deno.ts`.
We'll use our own fetch implementation to proxy requests through our server. This is a simple example, but you can use any fetch implementation you want.
## Example
**NOTE**: Build the library before running the examples.
```ts
import { Innertube } from "youtubei.js/web.bundle.min";
Web application:
const yt = await Innertube.create({
fetch: async (input, init) => {
// url
const url = typeof input === 'string'
? new URL(input)
: input instanceof URL
? input
: new URL(input.url);
// transform the url for use with our proxy
url.searchParams.set('__host', url.host);
url.host = 'localhost:8080';
url.protocol = 'http';
const headers = init?.headers
? new Headers(init.headers)
: input instanceof Request
? input.headers
: new Headers();
// now serialize the headers
url.searchParams.set('__headers', JSON.stringify([...headers]));
// copy over the request
const request = new Request(
url,
input instanceof Request ? input : undefined,
);
headers.delete('user-agent');
// fetch the url
return fetch(request, init ? {
...init,
headers
} : {
headers
});
},
cache: new UniversalCache(),
});
```shell
cd examples/browser/web
npm install
npm run dev
```
After that, you can use the library as normal.
## Example
We've got a full example in `examples/browser/web` using vite.
Proxy:
```shell
deno run --allow-net --allow-read examples/browser/proxy/deno.ts
```