active_hashcash 0.6.2 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5e43f6be8372678e15c9a19647a195a37d52a2b1fe8de8f3bf51847dfd513b6
4
- data.tar.gz: c54df83ac36629327f1bf23b125c1726133cc652c32e271e216e53a57ff4fbe9
3
+ metadata.gz: cb6b667de99d773ec58e9fe9a45f63085f3215600ef7a370c215cda854380856
4
+ data.tar.gz: f6d2c14d1c15c430784bf164f16c8c63ae533da821f951c5cd1f5c00ea39279b
5
5
  SHA512:
6
- metadata.gz: 42ff8c4e0ad05d8675c2f8e48b5ca234c230657725488fbfa0abecf43e6d81d7d66210ac285b5b0baa5d2f2f25b0a86e0cd54f2816b50a86b37a63333ded9cff
7
- data.tar.gz: d964f60754e9b823ec7e54b577498f9245fbb855aa1c2b33db0a97236faac73895b1f38c11a9420c78236ab0b8f4b69e79c50cb6d9a2fe30bfbd459aa74a4154
6
+ metadata.gz: 290b6a154e9395302d7b51d5cd45fa89adcc54b1e6f3a9f65a381afeb06775230a927f084c064d7c5998994df1d2e8dbce432efc236d59964ad99fa102e94bc0
7
+ data.tar.gz: abb0d050396b1aa6ea43a8d25c091e20ab6bfff1518d13af53344547d051ee682c150c73feac2e9a580022f65e798829e826ec3c4933ee5867180e20267e8eb8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog of ActiveHashcash
2
2
 
3
+ ## 0.6.3 (2026-11-15)
4
+
5
+ - Make `Hashcash.setup()` idempotent so it can be called again after navigations #26
6
+ - Fix lost original submit button text with Turbo Drive #29
7
+
3
8
  ## 0.6.2 (2026-11-13)
4
9
 
5
10
  - Fix #25 Stop adding lib/ to asset paths so Propshaft doesn't publish Ruby sources (@Spone)
data/README.md CHANGED
@@ -84,6 +84,8 @@ Or, link hashcash to your JavaScript manifest and load it to your head.
84
84
  <%= javascript_include_tag "hashcash", "data-turbo-track": "reload", defer: true %>
85
85
  ```
86
86
 
87
+ `Hashcash.setup()` runs when the script loads and is safe to call again: it will not mint twice for the same input. If the form appears after a navigation without reloading the script, call `Hashcash.setup()` yourself.
88
+
87
89
  The hashcash stamp will be set in the hidden input once computed and the submit button enabled.
88
90
 
89
91
  To customize behaviours, you can override methods of ActiveHashcash module.
@@ -1,18 +1,16 @@
1
1
  // http://www.hashcash.org/docs/hashcash.html
2
2
  // <input type="hidden" name="hashcash" data-hashcash="{resource: 'site.example', bits: 16}"/>
3
- Hashcash = function(input) {
3
+ Hashcash = window.Hashcash || function(input) {
4
4
  options = JSON.parse(input.getAttribute("data-hashcash"))
5
5
  Hashcash.disableParentForm(input, options)
6
6
  input.dispatchEvent(new CustomEvent("hashcash:mint", {bubbles: true}))
7
7
 
8
8
  Hashcash.mint(options.resource, options, function(stamp) {
9
- // Guard against Turbo navigation: if the input is no longer in the DOM,
10
- // the user has navigated away and we should silently discard the result.
11
- if (!input.isConnected) return
12
-
13
- input.value = stamp.toString()
14
- Hashcash.enableParentForm(input, options)
15
- input.dispatchEvent(new CustomEvent("hashcash:minted", {bubbles: true, detail: {stamp: stamp}}))
9
+ if (input.isConnected) {
10
+ input.value = stamp.toString()
11
+ Hashcash.enableParentForm(input, options)
12
+ input.dispatchEvent(new CustomEvent("hashcash:minted", {bubbles: true, detail: {stamp: stamp}}))
13
+ }
16
14
  })
17
15
 
18
16
  this.input = input
@@ -20,11 +18,15 @@ Hashcash = function(input) {
20
18
  }
21
19
 
22
20
  Hashcash.setup = function() {
23
- if (document.readyState != "loading") {
24
- var input = document.querySelector("input#hashcash")
25
- input && new Hashcash(input)
26
- } else
21
+ if (document.readyState == "loading") {
27
22
  document.addEventListener("DOMContentLoaded", Hashcash.setup)
23
+ } else {
24
+ var input = document.querySelector("input#hashcash")
25
+ if (input && !input.dataset.hashcashStarted) {
26
+ input.dataset.hashcashStarted = "1"
27
+ new Hashcash(input)
28
+ }
29
+ }
28
30
  }
29
31
 
30
32
  // Terminate the active worker and clean up its Blob URL.
@@ -39,33 +41,15 @@ Hashcash.cleanup = function() {
39
41
  }
40
42
  }
41
43
 
42
- // Turbo Drive: terminate the worker when navigating away so it doesn't
43
- // complete after the page has changed, and restore the form state for
44
- // Turbo's page cache so the snapshot doesn't have disabled buttons.
45
- document.addEventListener("turbo:before-visit", Hashcash.cleanup)
46
- document.addEventListener("turbo:before-cache", function() {
47
- Hashcash.cleanup()
48
- var input = document.querySelector("input#hashcash")
49
- if (input && input.form) {
50
- input.value = ""
51
- input.form.querySelectorAll("[type=submit]").forEach(function(submit) {
52
- if (submit.originalValue) {
53
- Hashcash.setSubmitText(submit, submit.originalValue)
54
- }
55
- submit.disabled = null
56
- })
57
- }
58
- })
59
-
60
44
  Hashcash.setSubmitText = function(submit, text) {
61
45
  if (!text) {
62
46
  return
63
47
  }
64
48
  if (submit.tagName == "BUTTON") {
65
- !submit.originalValue && (submit.originalValue = submit.innerHTML)
49
+ !submit.dataset.originalValue && (submit.dataset.originalValue = submit.innerHTML)
66
50
  submit.innerHTML = text
67
51
  } else {
68
- !submit.originalValue && (submit.originalValue = submit.value)
52
+ !submit.dataset.originalValue && (submit.dataset.originalValue = submit.value)
69
53
  submit.value = text
70
54
  }
71
55
  }
@@ -79,7 +63,7 @@ Hashcash.disableParentForm = function(input, options) {
79
63
 
80
64
  Hashcash.enableParentForm = function(input, options) {
81
65
  input.form.querySelectorAll("[type=submit]").forEach(function(submit) {
82
- Hashcash.setSubmitText(submit, submit.originalValue)
66
+ Hashcash.setSubmitText(submit, submit.dataset.originalValue)
83
67
  submit.disabled = null
84
68
  })
85
69
  }
@@ -1,3 +1,3 @@
1
1
  module ActiveHashcash
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.3"
3
3
  end
@@ -151,7 +151,9 @@ module ActiveHashcash
151
151
  # Override me for your own needs.
152
152
  end
153
153
 
154
- # Call it inside the form that have to be protected and don't forget to initialize the JavaScript Hashcash.setup().
154
+ # Call it inside the form that have to be protected.
155
+ # hashcash.js calls Hashcash.setup() on load; call it again after navigations that
156
+ # insert the field without reloading the script. setup is idempotent per input.
155
157
  # Unless you need something really special, you should not need to override this method.
156
158
  #
157
159
  # <% form_for model do |form| %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_hashcash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Bernard
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  - !ruby/object:Gem::Version
108
108
  version: '0'
109
109
  requirements: []
110
- rubygems_version: 4.0.10
110
+ rubygems_version: 4.0.20
111
111
  specification_version: 4
112
112
  summary: Protect Rails applications against bots and brute force attacks without annoying
113
113
  humans.