From: Ardis Lu Date: Sat, 21 Jan 2023 07:50:51 +0000 (-0800) Subject: Add buttcoin X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=c63472c7e554f57cf5230e9fdace0a3984422aba;p=cloudflare-workers Add buttcoin --- diff --git a/README.md b/README.md index 418f152..2cf38f7 100644 --- 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) +``` + +Snippet of the Wikipedia page for 'Bitcoin' but all instances of the word 'bitcoin' are replaced with 'buttcoin'. +

Command Line Utilities

## pwned diff --git a/buttcoin.png b/buttcoin.png new file mode 100644 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 index 0000000..a1988ff --- /dev/null +++ b/buttcoin/index.js @@ -0,0 +1,64 @@ +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)); + } + } + } +} + +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 index 0000000..ffe601f --- /dev/null +++ b/buttcoin/package.json @@ -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 index 0000000..03a71f1 --- /dev/null +++ b/buttcoin/wrangler.toml.example @@ -0,0 +1,4 @@ +name = 'buttcoin' +main = 'index.js' +compatibility_date = '2023-01-20' +route = { pattern = 'buttcoin.alu.bz', custom_domain = true } diff --git a/package.json b/package.json index cd35439..5b43dc8 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "uuid", "hash", "style", - "unroll" + "unroll", + "buttcoin" ] }