Skip to content

Commit

Permalink
Backport ideas by @mxmehl
Browse files Browse the repository at this point in the history
- Parameters can be positional or named.
- Arbitrary protocols can be given.
- Fingerprint Javascript code to make it unique.
  • Loading branch information
martignoni committed Apr 3, 2019
1 parent 9a71b4a commit e62294f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -21,9 +21,19 @@ This is not a standalone theme. It is a [Hugo](https://gohugo.io) theme componen
```go
{{< cloakemail "[email protected]" >}}
```
or
```go
{{< cloakemail address="[email protected]" >}}
```
or, if you want to specify a protocol,
```go
{{< cloakemail address="[email protected]" protocol="xmpp" >}}
```

### Credits

Copyright © 2019 onwards, Nicolas Martignoni [email protected].

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.
17 changes: 11 additions & 6 deletions layouts/shortcodes/cloakemail.html
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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>

0 comments on commit e62294f

Please sign in to comment.