chemicaljs 1.7.2 → 1.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +2 -364
  2. package/client/chemical.components.js +42 -25
  3. package/client/chemical.js +8 -0
  4. package/index.js +2 -0
  5. package/package.json +1 -1
  6. package/examples/example/README.md +0 -3
  7. package/examples/example/index.js +0 -21
  8. package/examples/example/package-lock.json +0 -3889
  9. package/examples/example/package.json +0 -24
  10. package/examples/example/public/index.html +0 -13
  11. package/examples/example-build/README.md +0 -5
  12. package/examples/example-build/build.js +0 -13
  13. package/examples/example-build/index.js +0 -16
  14. package/examples/example-build/package-lock.json +0 -3889
  15. package/examples/example-build/package.json +0 -25
  16. package/examples/example-build/public/index.html +0 -13
  17. package/examples/example-components/README.md +0 -3
  18. package/examples/example-components/index.js +0 -16
  19. package/examples/example-components/package-lock.json +0 -3889
  20. package/examples/example-components/package.json +0 -24
  21. package/examples/example-components/public/index.html +0 -45
  22. package/examples/example-styled/README.md +0 -3
  23. package/examples/example-styled/index.js +0 -16
  24. package/examples/example-styled/package-lock.json +0 -3889
  25. package/examples/example-styled/package.json +0 -24
  26. package/examples/example-styled/public/index.html +0 -24
  27. package/examples/example-styled/public/style.css +0 -82
  28. package/examples/example-vite/README.md +0 -9
  29. package/examples/example-vite/index.html +0 -12
  30. package/examples/example-vite/index.js +0 -16
  31. package/examples/example-vite/package-lock.json +0 -4834
  32. package/examples/example-vite/package.json +0 -19
  33. package/examples/example-vite/src/app.jsx +0 -14
  34. package/examples/example-vite/src/main.jsx +0 -4
  35. package/examples/example-vite/vite.config.js +0 -10
  36. package/update.bash +0 -16
package/README.md CHANGED
@@ -1,370 +1,8 @@
1
1
  # Chemical
2
2
  Easily create your own web proxy with no experience required.
3
3
 
4
- > [!IMPORTANT]
5
- > I am in the process of creating a documentation site. These docs might be outdated.
6
-
7
- ## Setup
8
-
9
- A simple example can be found in [`/example/`](https://github.com/chemicaljs/chemical/tree/main/examples/example).
10
-
11
- A example with the vite plugin can be found in [`/example-vite/`](https://github.com/chemicaljs/chemical/tree/main/examples/example-vite).
12
-
13
- A example with all the components can be found in [`/example-components/`](https://github.com/chemicaljs/chemical/tree/main/examples/example-components).
14
-
15
- A real world styled example can be found in [`/example-styled/`](https://github.com/chemicaljs/chemical/tree/main/examples/example-styled).
16
-
17
- A example with a build command and an external wisp server can be found in [`/example-build/`](https://github.com/chemicaljs/chemical/tree/main/examples/example-build).
18
-
19
-
20
- ### Server
21
-
22
- Create a new Node.js project and create a script file for the server.
23
-
24
- 1. Install Chemical `npm install chemicaljs`.
25
-
26
- 2. Import `ChemicalServer` and create a new server.
27
-
28
- ```js
29
- import { ChemicalServer } from "chemicaljs";
30
-
31
- const [app, listen] = new ChemicalServer();
32
- ```
33
-
34
- You can pass options to disable proxy services and set the default service.
35
-
36
- ```js
37
- const [app, listen] = new ChemicalServer({
38
- default: "rammerhead",
39
- uv: true,
40
- scramjet: false,
41
- rammerhead: true,
42
- hostname_blacklist: [ /google\.com/, /reddit\.com/ ],
43
- hostname_whitelist: [ /example\.com/ ]
44
- });
45
- ```
46
-
47
- `hostname_whitelist` overrides `hostname_blacklist` if you try to set them both.
48
-
49
- 3. Use `app` which is an express app. You may need to import express for certain APIs.
50
-
51
- ```js
52
- chemical.get("/", function(req, res){
53
- res.send("Hello World");
54
- });
55
- ```
56
-
57
- 4. Use `app.serveChemical()` to serve Chemical routes.
58
-
59
- 5. Use `serveChemical` after main routes but before 404 routes. Example 404 page.
60
-
61
- ```js
62
- app.use((req, res) => {
63
- res.status(404);
64
- res.send("404 Error");
65
- });
66
- ```
67
-
68
- 4. Use `listen` on a port of your choosing.
69
-
70
- ```js
71
- listen(3000);
72
- ```
73
-
74
- Below is an example of a simple backend. This example will setup Chemical and serve the "public" folder along with the `index.html` file as `/` and `.html` files without the extension.
75
-
76
-
77
- ```js
78
- import { ChemicalServer } from "chemicaljs";
79
- import express from "express";
80
-
81
- const [app, listen] = new ChemicalServer();
82
- const port = process.env.PORT || 3000;
83
-
84
- app.use(express.static("public", {
85
- index: "index.html",
86
- extensions: ["html"]
87
- }));
88
-
89
- app.serveChemical();
90
-
91
- app.use((req, res) => {
92
- res.status(404);
93
- res.send("404 Error");
94
- });
95
-
96
- listen(port, () => {
97
- console.log(`Chemical demo listening on port ${port}`);
98
- });
99
- ```
100
-
101
- ### Build
102
-
103
- Want to use Chemical without using a custom wisp and/or rammerhead server or without a server at all?
104
-
105
- Using the build command you can clone all needed assets into your build folder (like BareMux, Libcurl, and proxies enabled). Note that UV and Scramjet will need an external wisp server to function if you do not provide your own and Rammerhead will not work without your own Rammerhead server running.
106
-
107
- 1. Import `ChemicalBuild` and create a new build.
108
-
109
- ```js
110
- const build = new ChemicalBuild({
111
- path: "dist",
112
- default: "uv",
113
- uv: true,
114
- scramjet: true,
115
- rammerhead: false,
116
- });
117
- ```
118
-
119
- 2. Use `build.write()` to write into the build path.
120
-
121
- 3. Use `build.write(true)` to first empty the build path.
122
-
123
- Below if a full example of building Chemical.
124
-
125
- ```js
126
- const build = new ChemicalBuild({
127
- path: "dist",
128
- default: "uv",
129
- uv: true,
130
- scramjet: true,
131
- rammerhead: false,
132
- });
133
-
134
- await build.write(true);
135
- ```
136
-
137
- ### Client
138
-
139
- In your project create a folder to store your static assets. Create an index.html file which will be the homepage of your website.
140
-
141
- 1. Add the Chemical script to the top of your page. This will load all needed scripts for Chemical and other packages.
142
-
143
- ```html
144
- <script src="/chemical.js"></script>
145
- ```
146
-
147
- If you want to set the wisp server to an external server just change the `wisp` attribute.
148
-
149
- ```html
150
- <script data-wisp="wss://wisp.mercurywork.shop/" src="/chemical.js"></script>
151
- ```
152
-
153
- If you want to set the transport just change the `transport` attribute. Choose `libcurl` (default becuase it supports Firefox) or `epoxy`.
154
-
155
- ```html
156
- <script data-transport="epoxy" src="/chemical.js"></script>
157
- ```
158
-
159
- 2. In a inline script or javascript file, encode a URL with Chemical using the async function `chemical.encode`.
160
-
161
- ```js
162
- await chemical.encode("https://example.com")
163
- ```
164
-
165
- Optional: Change service to `uv`, `scramjet`, or `rammerhead`. Defaults to `uv` or server option.
166
-
167
- ```js
168
- await chemical.encode("https://example.com", {
169
- service: "rammerhead"
170
- })
171
- ```
172
-
173
- 3. To decode a URL use `chemical.decode`
174
-
175
- ```js
176
- const encodedURL = await chemical.encode("https://example.com")
177
- const decodedURL = await chemical.decode(encodedURL)
178
- ```
179
-
180
- 4. Set `autoHttps` to make `example.com` > `https://example.com`
181
-
182
- ```js
183
- await chemical.encode("example.com", {
184
- autoHttps: true
185
- })
186
- ```
187
-
188
- 5. Use a search engine with the `searchEngine` property. Use a search engine URL with "%s" in place of query.
189
-
190
- ```js
191
- await chemical.encode("cheese", {
192
- searchEngine: "https://www.google.com/search?q=%s"
193
- })
194
- ```
195
-
196
- 6. You may want to check if Chemical has loaded before encoding a URL.
197
-
198
- ```js
199
- if (window.chemical.loaded) {
200
- //Chemical is loaded
201
- }
202
- ```
203
-
204
- ```js
205
- window.addEventListener("chemicalLoaded", function(e) {
206
- //Chemical has loaded
207
- });
208
- ```
209
-
210
- 7. Change the transport with `chemical.setTransport`
211
-
212
- ```js
213
- await chemical.setTransport("libcurl") //libcurl or epoxy
214
- ```
215
-
216
- 8. Change the Wisp URL with `chemical.setWisp`
217
-
218
- ```js
219
- await chemical.setWisp("wss://wisp.mercurywork.shop/")
220
- ```
221
-
222
- Below is a simple example of a simple input that redirects to the encoded URL when the user presses enter. It checks if there is any input and if Chemical has loaded before loading.
223
-
224
- ```html
225
- <h1>Chemical Example</h1>
226
- <input id="search" placeholder="Enter URL">
227
-
228
- <script src="/chemical.js"></script>
229
- <script>
230
- const search = document.getElementById("search");
231
-
232
- search.addEventListener("keydown", async function (e) {
233
- if (e.key == "Enter" && chemical.loaded && e.target.value) {
234
- window.location = await chemical.encode(e.target.value)
235
- }
236
- })
237
- </script>
238
- ```
239
-
240
- 9. Fetch websites through Wisp using `chemical.fetch`. This works the same as the fetch API.
241
-
242
- 10. Get search suggestions via DuckDuckGo using `chemical.getSuggestions`
243
-
244
- ```js
245
- await chemical.getSuggestions("google")
246
- ```
247
-
248
- ## Vite Plugin
249
-
250
- 1. Create a new vite app and open `vite.config.js` or `vite.config.ts`
251
-
252
- 2. Import `ChemicalVitePlugin` and add it to plugins.
253
-
254
- ```js
255
- import { defineConfig } from "vite"
256
- import { ChemicalVitePlugin } from "chemicaljs"
257
-
258
- export default defineConfig({
259
- plugins: [/*Other plugins*/ChemicalVitePlugin()],
260
- })
261
- ```
262
-
263
- You can pass options to just like on the main server.
264
-
265
- ```js
266
- export default defineConfig({
267
- plugins: [
268
- ChemicalVitePlugin({
269
- default: "rammerhead",
270
- uv: true,
271
- scramjet: false,
272
- rammerhead: true,
273
- hostname_blacklist: [ /google\.com/, /reddit\.com/ ],
274
- hostname_whitelist: [ /example\.com/ ]
275
- })
276
- ],
277
- })
278
- ```
279
-
280
- ## Components
281
- Setup a proxy site with easy HTML components.
282
-
283
- 1. Add the Chemical components along with the main script to the top of your page.
284
-
285
- ```html
286
- <script src="/chemical.js"></script>
287
- <script src="/chemical.components.js"></script>
288
- ```
289
-
290
- 2. Now add any component to your site!
291
-
292
- ### Link
293
-
294
- An anchor link but it automatically encodes the URL.
295
-
296
- ```html
297
- <a href="https://example.com" is="chemical-link">Link</a>
298
- ```
299
-
300
- You can style the link as chemical is loading.
301
-
302
- ```css
303
- a[data-chemical-loading="true"] {
304
- cursor: wait;
305
- }
306
- ```
307
-
308
- ### Basic Input
309
-
310
- Opens in the current tab when the enter key is pressed.
311
-
312
- ```html
313
- <input target="_self" placeholder="Enter URL" is="chemical-input">
314
- ```
315
-
316
- Opens in current tab when the enter key is pressed.
317
-
318
- ```html
319
- <input target="_blank" placeholder="Enter URL" is="chemical-input">
320
- ```
321
-
322
- Custom action when the enter key is pressed. Change `action` to your function name. The first parameter of the action will be the encoded URL.
323
-
324
- ```html
325
- <input action="logURL" placeholder="Enter URL" is="chemical-input">
326
- <script>
327
- function logURL(url) {
328
- console.log(url)
329
- }
330
- </script>
331
- ```
332
-
333
- ### Input with Button
334
-
335
- Opens when the enter key is pressed or button is clicked. Set the `for` attribute to the `id` of the button.
336
-
337
- ```html
338
- <input id="my-input" target="_blank" placeholder="Enter URL" is="chemical-input">
339
- <button for="my-input" is="chemical-button">Go!</button>
340
- ```
341
-
342
- ### With iframe
343
-
344
- A hidden iframe that is shown when the enter key is pressed. Set the `frame` attribute to the `id` of the iframe.
345
-
346
- ```html
347
- <input frame="my-iframe" placeholder="Enter URL" is="chemical-input">
348
- <iframe id="my-iframe" is="chemical-iframe"></iframe>
349
- ```
350
-
351
- A hidden iframe that is shown when the enter key is pressed. Includes controls the are hidden when the iframe is hidden and can control websites in the iframe as well as hiding the iframe and controls.
352
-
353
- Set the `frame` attribute to the `id` of the iframe.
354
- Set the `controls` attribute to the `id` of the controls.
355
-
356
- Set the second parameter of `chemicalAction` to the `id` of the iframe.
357
-
358
- ```html
359
- <input frame="my-iframe-2" placeholder="Enter URL" is="chemical-input">
360
- <section id="my-controls-2" is="chemical-controls">
361
- <button onclick="chemicalAction('back', 'my-iframe-2')">←</button>
362
- <button onclick="chemicalAction('forward', 'my-iframe-2')">→</button>
363
- <button onclick="chemicalAction('reload', 'my-iframe-2')">⟳</button>
364
- <button onclick="chemicalAction('close', 'my-iframe-2')">🗙</button>
365
- </section>
366
- <iframe controls="my-controls-2" id="my-iframe-2" is="chemical-iframe"></iframe>
367
- ```
4
+ ## Docs
5
+ You can find documentation for Chemical on our website [https://chemicaljs.github.io](chemicaljs.github.io).
368
6
 
369
7
  ## License
370
8
  Chemical uses the AGPL 3.0 license.
@@ -4,16 +4,23 @@ class ChemicalInput extends HTMLInputElement {
4
4
  }
5
5
  connectedCallback() {
6
6
  this.addEventListener("keydown", async function (e) {
7
- if (e.key == "Enter" && window.chemicalLoaded && e.target.value) {
8
- let encodedURL = await window.chemicalEncode(e.target.value)
9
- let action = this.getAttribute("action")
10
- let target = this.getAttribute("target")
11
- let frame = this.getAttribute("frame")
7
+ if (e.key == "Enter" && window.chemical.loaded && e.target.value) {
8
+ let service = this.dataset.service || "uv";
9
+ let autoHttps = this.dataset.autoHttps !== undefined ? true : false;
10
+ let searchEngine = this.dataset.searchEngine;
11
+ let action = this.dataset.action
12
+ let target = this.dataset.target
13
+ let frame = this.dataset.frame
14
+ let encodedURL = await chemical.encode(e.target.value, {
15
+ service,
16
+ autoHttps,
17
+ searchEngine
18
+ })
12
19
 
13
20
  if (frame) {
14
- let forFrame = document.getElementById(this.getAttribute("frame"))
15
- forFrame.src = encodedURL
16
- forFrame.setAttribute("open", "true")
21
+ let forFrame = document.getElementById(frame)
22
+ forFrame.src = encodedURL;
23
+ forFrame.setAttribute("data-open", "true")
17
24
  }
18
25
 
19
26
  if (action) {
@@ -38,7 +45,7 @@ class ChemicalButton extends HTMLButtonElement {
38
45
  }
39
46
  connectedCallback() {
40
47
  this.addEventListener("click", function (e) {
41
- let forInput = document.getElementById(this.getAttribute("for"))
48
+ let forInput = document.getElementById(this.dataset.for)
42
49
 
43
50
  if (forInput) {
44
51
  forInput.dispatchEvent(new KeyboardEvent("keydown", {
@@ -50,40 +57,40 @@ class ChemicalButton extends HTMLButtonElement {
50
57
  }
51
58
 
52
59
  class ChemicalIFrame extends HTMLIFrameElement {
53
- static observedAttributes = ["open"];
60
+ static observedAttributes = ["data-open"];
54
61
  constructor() {
55
62
  super();
56
63
  }
57
64
  connectedCallback() {
58
- let open = this.getAttribute("open")
65
+ let open = this.dataset.open
59
66
  this.style.display = open == "true" ? "" : "none"
60
67
  }
61
68
  attributeChangedCallback(name, oldValue, newValue) {
62
- if (name == "open") {
63
- let open = this.getAttribute("open")
69
+ if (name == "data-open") {
70
+ let open = this.dataset.open
64
71
  this.style.display = open == "true" ? "" : "none"
65
72
 
66
- let controls = document.getElementById(this.getAttribute("controls"))
73
+ let controls = document.getElementById(this.dataset.controls)
67
74
 
68
75
  if (controls) {
69
- controls.setAttribute("open", open)
76
+ controls.dataset.open = open
70
77
  }
71
78
  }
72
79
  }
73
80
  }
74
81
 
75
82
  class ChemicalControls extends HTMLElement {
76
- static observedAttributes = ["open"];
83
+ static observedAttributes = ["data-open"];
77
84
  constructor() {
78
85
  super();
79
86
  }
80
87
  connectedCallback() {
81
- let open = this.getAttribute("open")
88
+ let open = this.dataset.open
82
89
  this.style.display = open == "true" ? "" : "none"
83
90
  }
84
91
  attributeChangedCallback(name, oldValue, newValue) {
85
- if (name == "open") {
86
- let open = this.getAttribute("open")
92
+ if (name == "data-open") {
93
+ let open = this.dataset.open
87
94
  this.style.display = open == "true" ? "" : "none"
88
95
  }
89
96
  }
@@ -94,16 +101,26 @@ class ChemicalLink extends HTMLAnchorElement {
94
101
  super();
95
102
  }
96
103
  async connectedCallback() {
97
- let href = this.getAttribute("href");
98
- this.removeAttribute("href");
104
+ let href = this.dataset.href;
105
+ let service = this.dataset.service || "uv";
106
+ let autoHttps = this.dataset.autoHttps !== undefined ? true : false;
107
+ let searchEngine = this.dataset.searchEngine;
99
108
  this.dataset.chemicalLoading = "true";
100
109
 
101
- if (window.chemicalLoaded) {
102
- this.setAttribute("href", await chemicalEncode(href))
110
+ if (window.chemical.loaded) {
111
+ this.setAttribute("href", await chemical.encode(href, {
112
+ service,
113
+ autoHttps,
114
+ searchEngine
115
+ }))
103
116
  this.dataset.chemicalLoading = "false";
104
117
  } else {
105
118
  window.addEventListener("chemicalLoaded", async () => {
106
- this.setAttribute("href", await chemicalEncode(href))
119
+ this.setAttribute("href", await chemical.encode(href, {
120
+ service,
121
+ autoHttps,
122
+ searchEngine
123
+ }))
107
124
  this.dataset.chemicalLoading = "false";
108
125
  })
109
126
  }
@@ -131,7 +148,7 @@ function chemicalAction(action, frameID) {
131
148
  frame.contentWindow.location.reload()
132
149
  break;
133
150
  case "close":
134
- frame.setAttribute("open", "false")
151
+ frame.dataset.open = "false"
135
152
  frame.src = ""
136
153
  }
137
154
  }
@@ -228,6 +228,14 @@ window.chemical.encode = async function (url, config) {
228
228
  }
229
229
  }
230
230
 
231
+ if (config.service == undefined) {
232
+ config.service = defaultService;
233
+ }
234
+
235
+ if (config.autoHttps == undefined) {
236
+ config.autoHttps = false;
237
+ }
238
+
231
239
  if (url.match(/^https?:\/\//)) {
232
240
  return await encodeService(url, config.service);
233
241
  } else if (config.autoHttps == true && (url.includes(".") && !url.includes(" "))) {
package/index.js CHANGED
@@ -296,6 +296,8 @@ class ChemicalBuild {
296
296
  chemicalMain = "const scramjetEnabled = " + String(this.options.scramjet) + ";\n" + chemicalMain
297
297
  chemicalMain = "const rammerheadEnabled = " + String(this.options.rammerhead) + ";\n" + chemicalMain
298
298
 
299
+ chemicalMain = "(async () => {\n" + chemicalMain + "\n})();";
300
+
299
301
  writeFileSync(resolve(this.options.path, "chemical.js"), chemicalMain);
300
302
 
301
303
  let chemicalSW = await readFileSync(resolve(__dirname, "client/chemical.sw.js"), "utf8");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chemicaljs",
3
- "version": "1.7.2",
3
+ "version": "1.7.4",
4
4
  "description": "Easily create your own web proxy with no experience required.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,3 +0,0 @@
1
- # Chemical Example
2
-
3
- Run `npm install` to install dependencies and then `npm start` to start.
@@ -1,21 +0,0 @@
1
- import { ChemicalServer } from "chemicaljs";
2
- import express from "express";
3
-
4
- const [app, listen] = new ChemicalServer();
5
- const port = process.env.PORT || 3000;
6
-
7
- app.use(express.static("public", {
8
- index: "index.html",
9
- extensions: ["html"]
10
- }));
11
-
12
- app.serveChemical();
13
-
14
- app.use((req, res) => {
15
- res.status(404);
16
- res.send("404 Error");
17
- });
18
-
19
- listen(port, () => {
20
- console.log(`Chemical example listening on port ${port}`);
21
- });