chemicaljs 1.3.3 → 1.4.0

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.
package/README.md CHANGED
@@ -69,7 +69,7 @@ chemical.server.listen(port, () => {
69
69
 
70
70
  In your project create a folder to store your static assets. Create an index.html file which will be the homepage of your website.
71
71
 
72
- 1. Add the Chemical script to the top of your page. This will load all needed scripts for Ultraviolet and Chemical.
72
+ 1. Add the Chemical script to the top of your page. This will load all needed scripts for Chemical and other packages.
73
73
 
74
74
  ```html
75
75
  <script src="/chemical.js"></script>
@@ -93,7 +93,7 @@ If you want to set the transport just change the `transport` attribute. Choose `
93
93
  await window.chemicalEncode("https://example.com")
94
94
  ```
95
95
 
96
- Optional: Change service to `ultraviolet`, `scramjet`, or `rammerhead`. Defaults to `uv` or server option.
96
+ Optional: Change service to `uv`, `scramjet`, or `rammerhead`. Defaults to `uv` or server option.
97
97
 
98
98
  ```js
99
99
  await window.chemicalEncode("https://example.com", "rammerhead")
@@ -167,5 +167,78 @@ export default defineConfig({
167
167
  })
168
168
  ```
169
169
 
170
+ ## Components
171
+ Setup a proxy site with easy HTML components.
172
+
173
+ 1. Add the Chemical components along with the main script to the top of your page.
174
+
175
+ ```html
176
+ <script src="/chemical.js"></script>
177
+ <script src="/chemical.components.js"></script>
178
+ ```
179
+
180
+ 2. Now add any component to your site!
181
+
182
+ ### Basic Input
183
+
184
+ Opens in the current tab when the enter key is pressed.
185
+
186
+ ```html
187
+ <input target="_self" placeholder="Enter URL" is="chemical-input">
188
+ ``
189
+
190
+ Opens in current tab when the enter key is pressed.
191
+
192
+ ```html
193
+ <input target="_blank" placeholder="Enter URL" is="chemical-input">
194
+ ```
195
+
196
+ 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.
197
+
198
+ ```html
199
+ <input action="logURL" placeholder="Enter URL" is="chemical-input">
200
+ <script>
201
+ function logURL(url) {
202
+ console.log(url)
203
+ }
204
+ </script>
205
+ ```
206
+
207
+ ### Input with Button
208
+
209
+ Opens when the enter key is pressed or button is clicked. Set the `for` attribute to the `id` of the button.
210
+
211
+ ```html
212
+ <input id="my-input" target="_blank" placeholder="Enter URL" is="chemical-input">
213
+ <button for="my-input" is="chemical-button">Go!</button>
214
+ ```
215
+
216
+ ### With iframe
217
+
218
+ A hidden iframe that is shown when the enter key is pressed. Set the `frame` attribute to the `id` of the iframe.
219
+
220
+ ```html
221
+ <input frame="my-iframe" placeholder="Enter URL" is="chemical-input">
222
+ <iframe id="my-iframe" is="chemical-iframe"></iframe>
223
+ ```
224
+
225
+ 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.
226
+
227
+ Set the `frame` attribute to the `id` of the iframe.
228
+ Set the `controls` attribute to the `id` of the controls.
229
+
230
+ Set the second parameter of `chemicalAction` to the `id` of the iframe.
231
+
232
+ ```html
233
+ <input frame="my-iframe-2" placeholder="Enter URL" is="chemical-input">
234
+ <section id="my-controls-2" is="chemical-controls">
235
+ <button onclick="chemicalAction('back', 'my-iframe-2')">←</button>
236
+ <button onclick="chemicalAction('forward', 'my-iframe-2')">→</button>
237
+ <button onclick="chemicalAction('reload', 'my-iframe-2')">⟳</button>
238
+ <button onclick="chemicalAction('close', 'my-iframe-2')">🗙</button>
239
+ </section>
240
+ <iframe controls="my-controls-2" id="my-iframe-2" is="chemical-iframe"></iframe>
241
+ ```
242
+
170
243
  ## License
171
244
  Chemical Uses the MIT license.
@@ -0,0 +1,116 @@
1
+ class ChemicalInput extends HTMLInputElement {
2
+ constructor() {
3
+ super();
4
+ }
5
+ connectedCallback() {
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")
12
+
13
+ if (frame) {
14
+ let forFrame = document.getElementById(this.getAttribute("frame"))
15
+ forFrame.src = encodedURL
16
+ forFrame.setAttribute("open", "true")
17
+ }
18
+
19
+ if (action) {
20
+ window[action](encodedURL)
21
+ }
22
+
23
+ if (target) {
24
+ if (target == "_self") {
25
+ window.location = encodedURL
26
+ } else if (target == "_blank") {
27
+ window.open(encodedURL)
28
+ }
29
+ }
30
+ }
31
+ })
32
+ }
33
+ }
34
+
35
+ class ChemicalButton extends HTMLButtonElement {
36
+ constructor() {
37
+ super();
38
+ }
39
+ connectedCallback() {
40
+ this.addEventListener("click", function (e) {
41
+ let forInput = document.getElementById(this.getAttribute("for"))
42
+
43
+ if (forInput) {
44
+ forInput.dispatchEvent(new KeyboardEvent("keydown", {
45
+ key: "Enter"
46
+ }))
47
+ }
48
+ })
49
+ }
50
+ }
51
+
52
+ class ChemicalIFrame extends HTMLIFrameElement {
53
+ static observedAttributes = ["open"];
54
+ constructor() {
55
+ super();
56
+ }
57
+ connectedCallback() {
58
+ let open = this.getAttribute("open")
59
+ this.style.display = open == "true" ? "" : "none"
60
+ }
61
+ attributeChangedCallback(name, oldValue, newValue) {
62
+ if (name == "open") {
63
+ let open = this.getAttribute("open")
64
+ this.style.display = open == "true" ? "" : "none"
65
+
66
+ let controls = document.getElementById(this.getAttribute("controls"))
67
+
68
+ if (controls) {
69
+ controls.setAttribute("open", open)
70
+ }
71
+ }
72
+ }
73
+ }
74
+
75
+ class ChemicalControls extends HTMLElement {
76
+ static observedAttributes = ["open"];
77
+ constructor() {
78
+ super();
79
+ }
80
+ connectedCallback() {
81
+ let open = this.getAttribute("open")
82
+ this.style.display = open == "true" ? "" : "none"
83
+ }
84
+ attributeChangedCallback(name, oldValue, newValue) {
85
+ if (name == "open") {
86
+ let open = this.getAttribute("open")
87
+ this.style.display = open == "true" ? "" : "none"
88
+ }
89
+ }
90
+ }
91
+
92
+ customElements.define("chemical-input", ChemicalInput, { extends: "input" });
93
+ customElements.define("chemical-button", ChemicalButton, { extends: "button" });
94
+ customElements.define("chemical-iframe", ChemicalIFrame, { extends: "iframe" });
95
+ customElements.define("chemical-controls", ChemicalControls, { extends: "section" });
96
+
97
+ function chemicalAction(action, frameID) {
98
+ let frame = document.getElementById(frameID)
99
+
100
+ if (frame) {
101
+ switch (action) {
102
+ case "back":
103
+ frame.contentWindow.history.back()
104
+ break;
105
+ case "forward":
106
+ frame.contentWindow.history.forward()
107
+ break;
108
+ case "reload":
109
+ frame.contentWindow.location.reload()
110
+ break;
111
+ case "close":
112
+ frame.setAttribute("open", "false")
113
+ frame.src = ""
114
+ }
115
+ }
116
+ }
@@ -9,7 +9,7 @@
9
9
  "version": "1.0.0",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
- "chemicaljs": "^1.3.2",
12
+ "chemicaljs": "^1.3.3",
13
13
  "express": "^4.19.2"
14
14
  }
15
15
  },
@@ -1158,9 +1158,9 @@
1158
1158
  }
1159
1159
  },
1160
1160
  "node_modules/chemicaljs": {
1161
- "version": "1.3.2",
1162
- "resolved": "https://registry.npmjs.org/chemicaljs/-/chemicaljs-1.3.2.tgz",
1163
- "integrity": "sha512-3CBzTiNou/oFrDIN7gT7wSu5GSMTaRQUzdVDvztM9anqgW76v109W0oZXYuqQ4eW6+Cab2+t3iohC3H2mOGFUQ==",
1161
+ "version": "1.3.3",
1162
+ "resolved": "https://registry.npmjs.org/chemicaljs/-/chemicaljs-1.3.3.tgz",
1163
+ "integrity": "sha512-4On6o/E3C5fmrAFBno6zLc1ca3VQZv+tFz+rPP5II1tcka4d14iT2zrxZS96jHgStQBQoihdgPtmGGMPkZ4OAw==",
1164
1164
  "dependencies": {
1165
1165
  "@mercuryworkshop/bare-mux": "^2.0.1",
1166
1166
  "@mercuryworkshop/epoxy-transport": "^2.1.3",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "homepage": "https://github.com/chemicaljs/chemical#readme",
20
20
  "dependencies": {
21
- "chemicaljs": "^1.3.2",
21
+ "chemicaljs": "^1.3.3",
22
22
  "express": "^4.19.2"
23
23
  }
24
24
  }
@@ -0,0 +1,3 @@
1
+ # Chemical Example Components
2
+
3
+ Run `npm install` to install dependencies and then `npm start` to start.
@@ -0,0 +1,14 @@
1
+ import { ChemicalServer } from "chemicaljs";
2
+ import express from "express";
3
+
4
+ const chemical = new ChemicalServer();
5
+ const port = process.env.PORT || 3000;
6
+
7
+ chemical.app.use(express.static("public", {
8
+ index: "index.html",
9
+ extensions: ["html"]
10
+ }));
11
+
12
+ chemical.server.listen(port, () => {
13
+ console.log(`Chemical example components listening on port ${port}`);
14
+ });