diff --git a/README.md b/README.md index 9b5dd95..8f0c2d4 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,10 @@ then it will listen on port 8383. Then you can make requests like: `http://localhost:8383/?url=https://duckduckgo.com` +To primitively rewrite relative hrefs to absolute ones: + +`http://localhost:8383/?url=https://duckduckgo.com?rewrite=1` + ## Install as a daemon ``` diff --git a/index.mjs b/index.mjs index c7ccce4..0a0a1cd 100755 --- a/index.mjs +++ b/index.mjs @@ -6,9 +6,16 @@ import axios from "axios"; const router = new Router(); +const href_regex = /href="\/"/g; + router.get("/", async (ctx) => { const url = ctx.query.url; + const rewrite = ctx.query.url; const response = await axios.get(url); + if (response.data.replace && rewrite) { + const origin = new URL(url).origin; + response.data = response.data.replace(href_regex, `href="${origin}/`); + } ctx.body = response.data; });