]> git.ardis.lu - cloudflare-workers/commitdiff
Apply SetBase and UpdateLink to all proxies
authorArdis Lu <ardisglu@gmail.com>
Tue, 27 Jun 2023 03:24:32 +0000 (20:24 -0700)
committerArdis Lu <ardisglu@gmail.com>
Tue, 27 Jun 2023 03:24:32 +0000 (20:24 -0700)
buttcoin/index.js
eruda/index.js
proxy/index.js
style/index.js

index fc398bfb475aed63652072dacff483338c3baa2c..b883fcfe68688dd5793faa667849aa94df5d9e03 100644 (file)
@@ -1,25 +1,5 @@
 import { decode } from 'html-entities';
-
-class SetBase {
-  constructor(origin) {
-    this.origin = origin;
-  }
-
-  element(element) {
-    element.prepend(`<base href="${this.origin}" />`, { 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 = '';
index 25085ad7070f27825b967cc57af445039bda6715..a5f4e81f379aff3bf3e0e8587b04229a6e53c6ca 100644 (file)
@@ -1,3 +1,5 @@
+import { SetBase, UpdateLink } from '../proxy';
+
 class InjectEruda {
   element(element) {
     element.prepend('<script src="//cdn.jsdelivr.net/npm/eruda"></script><script>eruda.init();</script>', { 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);
   }
 }
index 8e876784e419f01887f610e9a1bba9d6ac49652b..d7838998aa6b4ee4d39cc71e83618acd0c94a349 100644 (file)
@@ -1,7 +1,32 @@
+export class SetBase {
+  constructor(origin) {
+    this.origin = origin;
+  }
+
+  element(element) {
+    element.prepend(`<base href="${this.origin}" />`, { 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);
   }
 }
index e7dbde29915059822b07bacabd30148a3e1f51f6..a0cdfc3b41f1bd6f298e65e0d8d34ab5785677f3 100644 (file)
@@ -1,3 +1,5 @@
+import { SetBase, UpdateLink } from '../proxy';
+
 class InjectStyle {
   element(element) {
     element.prepend(`<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/sakura.css@1.4.1/css/sakura.min.css">`, { 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);
   }
 }
This page took 0.045294 seconds and 4 git commands to generate.