From 2f121610656086a514889c00a7aeeae44fd7a73d Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Mon, 26 Jun 2023 20:24:32 -0700 Subject: [PATCH] Apply SetBase and UpdateLink to all proxies --- buttcoin/index.js | 22 +--------------------- eruda/index.js | 12 +++++++++--- proxy/index.js | 29 +++++++++++++++++++++++++++-- style/index.js | 12 +++++++++--- 4 files changed, 46 insertions(+), 29 deletions(-) diff --git a/buttcoin/index.js b/buttcoin/index.js index fc398bf..b883fcf 100644 --- a/buttcoin/index.js +++ b/buttcoin/index.js @@ -1,25 +1,5 @@ import { decode } from 'html-entities'; - -class SetBase { - constructor(origin) { - this.origin = origin; - } - - element(element) { - element.prepend(``, { html: true }); - } -} - -class UpdateLink { - element(element) { - for (const attrName of ['href', 'src']) { - const attrValue = element.getAttribute(attrName); - if (attrValue !== null && attrValue[0] === '/') { - element.setAttribute(attrName, attrValue.substring(1)); - } - } - } -} +import { SetBase, UpdateLink } from '../proxy'; class ReplaceBitcoin { #buffer = ''; diff --git a/eruda/index.js b/eruda/index.js index 25085ad..a5f4e81 100644 --- a/eruda/index.js +++ b/eruda/index.js @@ -1,3 +1,5 @@ +import { SetBase, UpdateLink } from '../proxy'; + class InjectEruda { element(element) { element.prepend('', { html: true }); @@ -7,9 +9,13 @@ class InjectEruda { export default { async fetch(request) { const url = /^http(s)?:\/\//i.test(request.url) ? new URL(request.url) : new URL(`https://${request.url}`); - const queryString = decodeURIComponent(url.search.substring(1)); + const remoteUrl = new URL(decodeURIComponent(url.search.substring(1))); - const response = await fetch(queryString); - return new HTMLRewriter().on('body', new InjectEruda()).transform(response); + const response = await fetch(remoteUrl); + return new HTMLRewriter() + .on('head', new SetBase(remoteUrl.origin)) + .on('link, script', new UpdateLink()) + .on('body', new InjectEruda()) + .transform(response); } } diff --git a/proxy/index.js b/proxy/index.js index 8e87678..d783899 100644 --- a/proxy/index.js +++ b/proxy/index.js @@ -1,7 +1,32 @@ +export class SetBase { + constructor(origin) { + this.origin = origin; + } + + element(element) { + element.prepend(``, { html: true }); + } +} + +export class UpdateLink { + element(element) { + for (const attrName of ['href', 'src']) { + const attrValue = element.getAttribute(attrName); + if (attrValue !== null && attrValue[0] === '/') { + element.setAttribute(attrName, attrValue.substring(1)); + } + } + } +} + export default { async fetch(request) { const url = /^http(s)?:\/\//i.test(request.url) ? new URL(request.url) : new URL(`https://${request.url}`); - const queryString = decodeURIComponent(url.search.substring(1)); - return await fetch(queryString); + const remoteUrl = new URL(decodeURIComponent(url.search.substring(1))); + const response = await fetch(remoteUrl); + return new HTMLRewriter() + .on('head', new SetBase(remoteUrl.origin)) + .on('link, script', new UpdateLink()) + .transform(response); } } diff --git a/style/index.js b/style/index.js index e7dbde2..a0cdfc3 100644 --- a/style/index.js +++ b/style/index.js @@ -1,3 +1,5 @@ +import { SetBase, UpdateLink } from '../proxy'; + class InjectStyle { element(element) { element.prepend(``, { html: true }); @@ -7,9 +9,13 @@ class InjectStyle { export default { async fetch(request) { const url = /^http(s)?:\/\//i.test(request.url) ? new URL(request.url) : new URL(`https://${request.url}`); - const queryString = decodeURIComponent(url.search.substring(1)); + const remoteUrl = new URL(decodeURIComponent(url.search.substring(1))); - const response = await fetch(queryString); - return new HTMLRewriter().on('html', new InjectStyle()).transform(response); + const response = await fetch(remoteUrl); + return new HTMLRewriter() + .on('head', new SetBase(remoteUrl.origin)) + .on('link, script', new UpdateLink()) + .on('html', new InjectStyle()) + .transform(response); } } -- 2.47.3