From 59d8674630b85c4c7e08650de78fc07b3f88a773 Mon Sep 17 00:00:00 2001 From: Yumine Sakura <31837214+asadahimeka@users.noreply.github.com> Date: Tue, 7 Feb 2023 14:28:14 +0800 Subject: [PATCH] docs: update readme --- readme.md | 16 ++++++++++++++++ readme.zh-cn.md | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/readme.md b/readme.md index 6e2d579..ff21b98 100755 --- a/readme.md +++ b/readme.md @@ -64,6 +64,22 @@ booru was built for Node.js, and is only officially supported for Node.js. Issue It's possible to use booru on the web using webpack (or similar), although your experience may vary. Some websites don't have the proper CORS headers, meaning that API requests to those sites from a browser will fail! This is not an issue I can fix in the package, and requires either that booru to add proper support themselves or for you to find a workaround for CORS. +For sites that do not support CORS, you can hack the code as follows, noting that this code should be placed before importing `@himeka/booru`. + +The CORS proxy needs to support calls of the form `https://cors.example.com/https://konachan.net/post.json`. + +```js +const _fetch = window.fetch +const proxy = 'https://cors.example.com/' // Replace with your own CORS proxy +window.fetch = (input, init) => { + let url = input.toString() + if (url.startsWith('https')) { + url = proxy + url + } + return _fetch(url, init) +} +``` + ## FAQ ### What are the properties of a `Post`? diff --git a/readme.zh-cn.md b/readme.zh-cn.md index 948c628..6edf1c9 100644 --- a/readme.zh-cn.md +++ b/readme.zh-cn.md @@ -65,6 +65,22 @@ booru 主要是为 Node.js 构建的,浏览器支持可能有限。 可以使用 webpack(或类似工具)在 web 上使用 booru,尽管体验可能会有所不同。 某些网站没有正确的 CORS 标头,这意味着从浏览器对这些网站的 API 请求将失败,需要自行解决跨域问题。 +对于不支持 CORS 的站点,可以按如下代码 hack 一下,注意这段代码要放到引用 `@himeka/booru` 之前。 + +CORS 代理需要支持 `https://cors.example.com/https://konachan.net/post.json` 形式的调用。 + +```js +const _fetch = window.fetch +const proxy = 'https://cors.example.com/' // 替换成你自己的 CORS 代理 +window.fetch = (input, init) => { + let url = input.toString() + if (url.startsWith('https')) { + url = proxy + url + } + return _fetch(url, init) +} +``` + ## FAQ ### `Post` 有什么属性?