]> git.ardis.lu - cloudflare-workers/commitdiff
Add style worker
authorArdis Lu <ardisglu@gmail.com>
Mon, 18 Oct 2021 02:02:25 +0000 (19:02 -0700)
committerArdis Lu <ardisglu@gmail.com>
Mon, 18 Oct 2021 02:02:25 +0000 (19:02 -0700)
README.md
package.json
style/index.js [new file with mode: 0644]
style/package.json [new file with mode: 0644]
style/wrangler.toml.example [new file with mode: 0644]

index d6e8793eed0470fafab39a450a18a31a1fef8dfd..475cc0bd626b026d3b902bc04903ebbebd3211b2 100644 (file)
--- 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`.
index a9dadcfcb621c9147bdf99d27ca7868964c59686..117ae00f98a0f2c595edc12cc01b4762d32cf8ef 100644 (file)
@@ -27,6 +27,6 @@
     "qr",
     "ip",
     "uuid",
-    "hash"
+    "style"
   ]
 }
diff --git a/style/index.js b/style/index.js
new file mode 100644 (file)
index 0000000..6747224
--- /dev/null
@@ -0,0 +1,17 @@
+class InjectStyle {
+  element(element) {
+    element.prepend(`<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/sakura.css@1.3.1/css/sakura.min.css">`, { 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 (file)
index 0000000..4e67145
--- /dev/null
@@ -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 (file)
index 0000000..4103906
--- /dev/null
@@ -0,0 +1,5 @@
+name = 'style'
+type = 'javascript'
+account_id = ''
+workers_dev = true
+route = 'example.com/*'
This page took 0.035482 seconds and 4 git commands to generate.