Backport ideas by @mxmehl
- Parameters can be positional or named. - Arbitrary protocols can be given. - Fingerprint Javascript code to make it unique.
This commit is contained in:
parent
492d616cf0
commit
d64666ce19
12
README.md
12
README.md
|
@ -4,7 +4,7 @@
|
|||
|
||||
## About
|
||||
|
||||
This is not a standalone theme. It is a [Hugo](https://gohugo.io) theme component providing a shortcode: `cloakemail` to cloak any e-mail address from spamming bots.
|
||||
This is not a standalone theme. It is a [Hugo](https://gohugo.io) theme component providing a shortcode: `cloakemail` to cloak any e-mail or other messaging (`xmpp`, `telegram`, etc.) or phone (`tel`) address from spamming bots.
|
||||
|
||||
## Usage
|
||||
|
||||
|
@ -21,9 +21,19 @@ This is not a standalone theme. It is a [Hugo](https://gohugo.io) theme componen
|
|||
```go
|
||||
{{< cloakemail "john.doe@example.com" >}}
|
||||
```
|
||||
or
|
||||
```go
|
||||
{{< cloakemail address="john.doe@example.com" >}}
|
||||
```
|
||||
or, if you want to specify a protocol,
|
||||
```go
|
||||
{{< cloakemail address="john.doe@example.com" protocol="xmpp" >}}
|
||||
```
|
||||
|
||||
### Credits
|
||||
|
||||
Copyright © 2019 onwards, Nicolas Martignoni nicolas@martignoni.net.
|
||||
|
||||
This theme component was possible because of the work done by [@danieka](https://github.com/danieka) in [this pull request](https://github.com/gohugoio/hugo/pull/3935).
|
||||
|
||||
Thanks to [@mxmehl](https://github.com/mxmehl) for ideas and explanations about other protocols and script fingerprinting.
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
{{- $parts := split (index .Params 0) "@" -}}
|
||||
{{/* Get address and protocol */}}
|
||||
{{- $address := .Get "address" | default (.Get 0) -}}
|
||||
{{- $protocol := .Get "protocol" | default "mailto" -}}
|
||||
{{- $parts := split $address "@" -}}
|
||||
{{- $user := (index $parts 0) -}}
|
||||
{{- $domain := (index $parts 1) -}}
|
||||
{{/* Compute md5 fingerprint */}}
|
||||
{{- $fingerprint := md5 (print (.Get "address") $protocol (index (seq 999 | shuffle) 0)) -}}
|
||||
<style type="text/css">
|
||||
.cloaked-e-mail:before {
|
||||
content:attr(data-domain) "\0040" attr(data-user);
|
||||
|
@ -9,12 +14,12 @@
|
|||
}
|
||||
</style>
|
||||
<span class="cloaked-e-mail" data-user="{{ range $index := seq (sub (len $user) 1) 0}}{{ substr $user $index 1}}{{ end }}" data-domain="{{ range $index := seq (sub (len $domain) 1) 0}}{{ substr $domain $index 1}}{{ end }}"></span>
|
||||
<script id="id">
|
||||
var scriptTag = document.getElementById("id");
|
||||
<script id="{{ $fingerprint }}">
|
||||
var scriptTag = document.getElementById("{{ $fingerprint }}");
|
||||
var link = document.createElement("a");
|
||||
var mail = "{{ range $index := seq (sub (len $user) 1) 0}}{{ substr $user $index 1}}{{ end }}".split('').reverse().join('') + "@" + "{{ range $index := seq (sub (len $domain) 1) 0}}{{ substr $domain $index 1}}{{ end }}".split('').reverse().join('');
|
||||
link.href = "mailto:" + mail;
|
||||
link.innerText = mail;
|
||||
var address = "{{ range $index := seq (sub (len $user) 1) 0}}{{ substr $user $index 1}}{{ end }}".split('').reverse().join('') + "@" + "{{ range $index := seq (sub (len $domain) 1) 0}}{{ substr $domain $index 1}}{{ end }}".split('').reverse().join('');
|
||||
link.href = {{ $protocol }} + ":" + address;
|
||||
link.innerText = address;
|
||||
scriptTag.parentElement.insertBefore(link, scriptTag.previousElementSibling);
|
||||
scriptTag.parentElement.removeChild(scriptTag.previousElementSibling)
|
||||
</script>
|
||||
|
|
Loading…
Reference in New Issue