chemicaljs 1.3.3 → 1.4.1

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
@@ -7,6 +7,9 @@ A simple example can be found in [`/example/`](https://github.com/chemicaljs/che
7
7
 
8
8
  A example with the vite plugin can be found in [`/example-vite/`](https://github.com/chemicaljs/chemical/tree/main/example-vite).
9
9
 
10
+ A example with all the components can be found in [`/example-components/`](https://github.com/chemicaljs/chemical/tree/main/example-components).
11
+
12
+
10
13
  ### Server
11
14
 
12
15
  Create a new Node.js project and create a script file for the server.
@@ -69,7 +72,7 @@ chemical.server.listen(port, () => {
69
72
 
70
73
  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
74
 
72
- 1. Add the Chemical script to the top of your page. This will load all needed scripts for Ultraviolet and Chemical.
75
+ 1. Add the Chemical script to the top of your page. This will load all needed scripts for Chemical and other packages.
73
76
 
74
77
  ```html
75
78
  <script src="/chemical.js"></script>
@@ -93,7 +96,7 @@ If you want to set the transport just change the `transport` attribute. Choose `
93
96
  await window.chemicalEncode("https://example.com")
94
97
  ```
95
98
 
96
- Optional: Change service to `ultraviolet`, `scramjet`, or `rammerhead`. Defaults to `uv` or server option.
99
+ Optional: Change service to `uv`, `scramjet`, or `rammerhead`. Defaults to `uv` or server option.
97
100
 
98
101
  ```js
99
102
  await window.chemicalEncode("https://example.com", "rammerhead")
@@ -137,7 +140,7 @@ Below is a simple example of a simple input that redirects to the encoded URL wh
137
140
  </script>
138
141
  ```
139
142
 
140
- ### Vite Plugin
143
+ ## Vite Plugin
141
144
 
142
145
  1. Create a new vite app and open `vite.config.js` or `vite.config.ts`
143
146
 
@@ -167,5 +170,78 @@ export default defineConfig({
167
170
  })
168
171
  ```
169
172
 
173
+ ## Components
174
+ Setup a proxy site with easy HTML components.
175
+
176
+ 1. Add the Chemical components along with the main script to the top of your page.
177
+
178
+ ```html
179
+ <script src="/chemical.js"></script>
180
+ <script src="/chemical.components.js"></script>
181
+ ```
182
+
183
+ 2. Now add any component to your site!
184
+
185
+ ### Basic Input
186
+
187
+ Opens in the current tab when the enter key is pressed.
188
+
189
+ ```html
190
+ <input target="_self" placeholder="Enter URL" is="chemical-input">
191
+ ```
192
+
193
+ Opens in current tab when the enter key is pressed.
194
+
195
+ ```html
196
+ <input target="_blank" placeholder="Enter URL" is="chemical-input">
197
+ ```
198
+
199
+ 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.
200
+
201
+ ```html
202
+ <input action="logURL" placeholder="Enter URL" is="chemical-input">
203
+ <script>
204
+ function logURL(url) {
205
+ console.log(url)
206
+ }
207
+ </script>
208
+ ```
209
+
210
+ ### Input with Button
211
+
212
+ Opens when the enter key is pressed or button is clicked. Set the `for` attribute to the `id` of the button.
213
+
214
+ ```html
215
+ <input id="my-input" target="_blank" placeholder="Enter URL" is="chemical-input">
216
+ <button for="my-input" is="chemical-button">Go!</button>
217
+ ```
218
+
219
+ ### With iframe
220
+
221
+ A hidden iframe that is shown when the enter key is pressed. Set the `frame` attribute to the `id` of the iframe.
222
+
223
+ ```html
224
+ <input frame="my-iframe" placeholder="Enter URL" is="chemical-input">
225
+ <iframe id="my-iframe" is="chemical-iframe"></iframe>
226
+ ```
227
+
228
+ 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.
229
+
230
+ Set the `frame` attribute to the `id` of the iframe.
231
+ Set the `controls` attribute to the `id` of the controls.
232
+
233
+ Set the second parameter of `chemicalAction` to the `id` of the iframe.
234
+
235
+ ```html
236
+ <input frame="my-iframe-2" placeholder="Enter URL" is="chemical-input">
237
+ <section id="my-controls-2" is="chemical-controls">
238
+ <button onclick="chemicalAction('back', 'my-iframe-2')">←</button>
239
+ <button onclick="chemicalAction('forward', 'my-iframe-2')">→</button>
240
+ <button onclick="chemicalAction('reload', 'my-iframe-2')">⟳</button>
241
+ <button onclick="chemicalAction('close', 'my-iframe-2')">🗙</button>
242
+ </section>
243
+ <iframe controls="my-controls-2" id="my-iframe-2" is="chemical-iframe"></iframe>
244
+ ```
245
+
170
246
  ## License
171
247
  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.4.0",
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.4.0",
1162
+ "resolved": "https://registry.npmjs.org/chemicaljs/-/chemicaljs-1.4.0.tgz",
1163
+ "integrity": "sha512-FyB/RVkT3c3prjQAVnU6NQaYPJFEG8XOuGz9N3tvtC4LneT6oCmjqmK5M5lU/y0baFviF+vSonaI3z3l5NxXBg==",
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.4.0",
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
+ });