]> git.ardis.lu - cloudflare-workers/commitdiff
Add buttcoin
authorArdis Lu <ardisglu@gmail.com>
Sat, 21 Jan 2023 07:50:51 +0000 (23:50 -0800)
committerArdis Lu <ardisglu@gmail.com>
Sat, 21 Jan 2023 07:50:51 +0000 (23:50 -0800)
README.md
buttcoin.png [new file with mode: 0644]
buttcoin/index.js [new file with mode: 0644]
buttcoin/package.json [new file with mode: 0644]
buttcoin/wrangler.toml.example [new file with mode: 0644]
package.json

index 418f15215efc9dc0bc242bf49a6ed0124de270ef..2cf38f7335508846266eea66138ee204d4a22001 100644 (file)
--- a/README.md
+++ b/README.md
@@ -137,6 +137,18 @@ https://x.y.workers.dev?https://www.instagram.com/p/{POST_ID}/
 > (Page with the Instagram post's embedded view in it)
 ```
 
+## buttcoin
+
+Returns a page where all instances of the word "bitcoin" are replaced with
+"buttcoin".
+
+```
+https://x.y.workers.dev?https://en.wikipedia.org/wiki/Bitcoin
+> (See image below)
+```
+
+<img alt="Snippet of the Wikipedia page for 'Bitcoin' but all instances of the word 'bitcoin' are replaced with 'buttcoin'." src="./buttcoin.png">
+
 <h1 align="center">Command Line Utilities</h1>
 
 ## pwned
diff --git a/buttcoin.png b/buttcoin.png
new file mode 100644 (file)
index 0000000..ab1f75a
Binary files /dev/null and b/buttcoin.png differ
diff --git a/buttcoin/index.js b/buttcoin/index.js
new file mode 100644 (file)
index 0000000..a1988ff
--- /dev/null
@@ -0,0 +1,64 @@
+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));
+      }
+    }
+  }
+}
+
+class ReplaceBitcoin {
+  #buffer = '';
+
+  text(text) {
+    this.#buffer += text.text;
+
+    if (text.lastInTextNode) {
+      const buttText = this.#buffer
+        .replaceAll('bitcoin', 'buttcoin')
+        .replaceAll('Bitcoin', 'Buttcoin')
+        .replaceAll('BITCOIN', 'BUTTCOIN')
+        .replaceAll(/bitcoin/gi, 'buttcoin'); // Any other mixed case just become buttcoin
+      text.replace(buttText);
+      this.#buffer = '';
+    }
+    else {
+      text.remove();
+    }
+  }
+}
+
+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));
+    let remoteUrl;
+    try {
+      remoteUrl = new URL(queryString);
+    }
+    catch {
+      return new Response('Invalid URL provided in query string.');
+    }
+
+    const response = await fetch(remoteUrl);
+
+    return new HTMLRewriter()
+      .on('head', new SetBase(remoteUrl.origin))
+      .on('link, script', new UpdateLink())
+      .on('html', new ReplaceBitcoin())
+      .transform(response);
+  }
+}
diff --git a/buttcoin/package.json b/buttcoin/package.json
new file mode 100644 (file)
index 0000000..ffe601f
--- /dev/null
@@ -0,0 +1,16 @@
+{
+  "name": "buttcoin",
+  "version": "0.2.0",
+  "description": "Reverse proxy which replaces all instances of the word \"bitcoin\" to \"buttcoin\".",
+  "license": "ISC",
+  "author": "Ardis Lu",
+  "private": true,
+  "scripts": {
+    "dev": "wrangler dev",
+    "publish": "wrangler publish"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/ardislu/cloudflare-workers.git"
+  }
+}
diff --git a/buttcoin/wrangler.toml.example b/buttcoin/wrangler.toml.example
new file mode 100644 (file)
index 0000000..03a71f1
--- /dev/null
@@ -0,0 +1,4 @@
+name = 'buttcoin'
+main = 'index.js'
+compatibility_date = '2023-01-20'
+route = { pattern = 'buttcoin.alu.bz', custom_domain = true }
index cd35439bd2bf691864c032f3277f5511d0656e5c..5b43dc887a1a92c9bf4c20eb35a57d8708bb279f 100644 (file)
@@ -26,6 +26,7 @@
     "uuid",
     "hash",
     "style",
-    "unroll"
+    "unroll",
+    "buttcoin"
   ]
 }
This page took 0.03998 seconds and 4 git commands to generate.