From: Ardis Lu Date: Mon, 18 Oct 2021 02:02:25 +0000 (-0700) Subject: Add style worker X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=c47fd9f3182948392f2b7b59715c69e862642341;p=cloudflare-workers Add style worker --- diff --git a/README.md b/README.md index d6e8793..475cc0b 100644 --- a/README.md +++ b/README.md @@ -183,3 +183,11 @@ Supported values are: ### `query` (alias `q`) The string you want to get the hash of. + +## style + +Reverse proxy which injects a basic CSS style sheet. + +### Usage + +Same as `proxy`. diff --git a/package.json b/package.json index a9dadcf..117ae00 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,6 @@ "qr", "ip", "uuid", - "hash" + "style" ] } diff --git a/style/index.js b/style/index.js new file mode 100644 index 0000000..6747224 --- /dev/null +++ b/style/index.js @@ -0,0 +1,17 @@ +class InjectStyle { + element(element) { + element.prepend(``, { html: true }); + } +} + +async function handleRequest(request) { + const url = new URL(request.url); + const queryString = decodeURIComponent(url.search.substring(1)); + + const response = await fetch(queryString); + return new HTMLRewriter().on('html', new InjectStyle()).transform(response); +} + +addEventListener('fetch', event => { + event.respondWith(handleRequest(event.request)); +}) diff --git a/style/package.json b/style/package.json new file mode 100644 index 0000000..4e67145 --- /dev/null +++ b/style/package.json @@ -0,0 +1,20 @@ +{ + "name": "style", + "version": "0.1.0", + "description": "Reverse proxy which injects a basic CSS style sheet.", + "private": true, + "main": "index.js", + "scripts": { + "login": "wrangler login", + "dev": "wrangler dev", + "preview": "wrangler preview", + "build": "wrangler build", + "publish": "wrangler publish" + }, + "author": "Ardis Lu", + "license": "ISC", + "repository": { + "type": "git", + "url": "https://github.com/ardislu/cloudflare-workers.git" + } +} diff --git a/style/wrangler.toml.example b/style/wrangler.toml.example new file mode 100644 index 0000000..4103906 --- /dev/null +++ b/style/wrangler.toml.example @@ -0,0 +1,5 @@ +name = 'style' +type = 'javascript' +account_id = '' +workers_dev = true +route = 'example.com/*'