From ff5f3bdffc857645a5e985bd7008e565664a91c3 Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Sat, 16 Oct 2021 12:20:12 +0200 Subject: [PATCH] Try to rewrite the output hrefs --- README.md | 4 ++++ index.mjs | 7 +++++++ 2 files changed, 11 insertions(+) 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; });