-
-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(examples): Use BgUtils to generate pot [skip ci]
+ Improve readme.
- Loading branch information
Showing
5 changed files
with
386 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
```ts | ||
import { Innertube } from "youtubei.js/web.bundle.min"; | ||
|
||
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, | ||
); | ||
## Example | ||
**NOTE**: Build the library before running the examples. | ||
|
||
headers.delete('user-agent'); | ||
Web application: | ||
|
||
// 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 | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
"vite": "^3.0.0" | ||
}, | ||
"dependencies": { | ||
"bgutils-js": "^1.1.0", | ||
"shaka-player": "^4.3.8" | ||
} | ||
} | ||
} |
Oops, something went wrong.