chemicaljs 1.7.2 → 1.7.3

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
@@ -49,7 +49,7 @@ const [app, listen] = new ChemicalServer({
49
49
  3. Use `app` which is an express app. You may need to import express for certain APIs.
50
50
 
51
51
  ```js
52
- chemical.get("/", function(req, res){
52
+ app.get("/", function(req, res){
53
53
  res.send("Hello World");
54
54
  });
55
55
  ```
@@ -294,9 +294,11 @@ Setup a proxy site with easy HTML components.
294
294
  An anchor link but it automatically encodes the URL.
295
295
 
296
296
  ```html
297
- <a href="https://example.com" is="chemical-link">Link</a>
297
+ <a data-href="https://example.com" is="chemical-link">Link</a>
298
298
  ```
299
299
 
300
+ You can also add `data-service="uv"`, `data-autoHttps`, and `data-search-engine="https://www.google.com/search?q=%s"`
301
+
300
302
  You can style the link as chemical is loading.
301
303
 
302
304
  ```css
@@ -310,19 +312,21 @@ a[data-chemical-loading="true"] {
310
312
  Opens in the current tab when the enter key is pressed.
311
313
 
312
314
  ```html
313
- <input target="_self" placeholder="Enter URL" is="chemical-input">
315
+ <input data-target="_self" placeholder="Enter URL" is="chemical-input">
314
316
  ```
315
317
 
318
+ You can also add `data-service="uv"`, `data-autoHttps`, and `data-search-engine="https://www.google.com/search?q=%s"`
319
+
316
320
  Opens in current tab when the enter key is pressed.
317
321
 
318
322
  ```html
319
- <input target="_blank" placeholder="Enter URL" is="chemical-input">
323
+ <input data-target="_blank" placeholder="Enter URL" is="chemical-input">
320
324
  ```
321
325
 
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.
326
+ Custom action when the enter key is pressed. Change `data-action` to your function name. The first parameter of the action will be the encoded URL.
323
327
 
324
328
  ```html
325
- <input action="logURL" placeholder="Enter URL" is="chemical-input">
329
+ <input data-action="logURL" placeholder="Enter URL" is="chemical-input">
326
330
  <script>
327
331
  function logURL(url) {
328
332
  console.log(url)
@@ -335,8 +339,8 @@ Custom action when the enter key is pressed. Change `action` to your function na
335
339
  Opens when the enter key is pressed or button is clicked. Set the `for` attribute to the `id` of the button.
336
340
 
337
341
  ```html
338
- <input id="my-input" target="_blank" placeholder="Enter URL" is="chemical-input">
339
- <button for="my-input" is="chemical-button">Go!</button>
342
+ <input id="my-input" data-target="_blank" placeholder="Enter URL" is="chemical-input">
343
+ <button data-for="my-input" is="chemical-button">Go!</button>
340
344
  ```
341
345
 
342
346
  ### With iframe
@@ -344,7 +348,7 @@ Opens when the enter key is pressed or button is clicked. Set the `for` attribut
344
348
  A hidden iframe that is shown when the enter key is pressed. Set the `frame` attribute to the `id` of the iframe.
345
349
 
346
350
  ```html
347
- <input frame="my-iframe" placeholder="Enter URL" is="chemical-input">
351
+ <input data-frame="my-iframe" placeholder="Enter URL" is="chemical-input">
348
352
  <iframe id="my-iframe" is="chemical-iframe"></iframe>
349
353
  ```
350
354
 
@@ -356,14 +360,14 @@ Set the `controls` attribute to the `id` of the controls.
356
360
  Set the second parameter of `chemicalAction` to the `id` of the iframe.
357
361
 
358
362
  ```html
359
- <input frame="my-iframe-2" placeholder="Enter URL" is="chemical-input">
363
+ <input data-frame="my-iframe-2" placeholder="Enter URL" is="chemical-input">
360
364
  <section id="my-controls-2" is="chemical-controls">
361
365
  <button onclick="chemicalAction('back', 'my-iframe-2')">←</button>
362
366
  <button onclick="chemicalAction('forward', 'my-iframe-2')">→</button>
363
367
  <button onclick="chemicalAction('reload', 'my-iframe-2')">⟳</button>
364
368
  <button onclick="chemicalAction('close', 'my-iframe-2')">🗙</button>
365
369
  </section>
366
- <iframe controls="my-controls-2" id="my-iframe-2" is="chemical-iframe"></iframe>
370
+ <iframe data-controls="my-controls-2" id="my-iframe-2" is="chemical-iframe"></iframe>
367
371
  ```
368
372
 
369
373
  ## 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(" "))) {