forked from alanhogan/email-address-de-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.email-address-de-obfuscator.js.coffee
executable file
·59 lines (53 loc) · 1.98 KB
/
jquery.email-address-de-obfuscator.js.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Plugin Email Address De-Obfuscator by Alan Hogan, 2013
#
# It’s public domain. Enjoy freely.
#
# Project home on GitHub: https://github.com/alanhogan/email-address-de-obfuscator
#
# There are no tests, but there’s a jsFiddle: http://jsfiddle.net/alanhogan/tyLtQ/
#
# Started from https://github.com/alanhogan/Coffeescript-jQuery-Plugin-Template
(($, window) ->
$.extend $.fn, deObfuscateEmailAddresses: (options) ->
@defaultOptions =
class: 'js-e'
dataKey: 'js-e-addr'
# Each replacement is a two-item array: search, replace.
replacements: [
# Using CoffeeScript's expanded regex syntax. It is love.
[
///
\s* # optional whitespace, greedily
\[at\] # Literal hard brackets around the string "at"
\s+ # optional whitespace, greedily
///i, # Modifier: Case-insensitive (match AT, at, At…)
'@'
],
[
///
\s* # optional whitespace, greedily
\[dot\] # Literal hard brackets around the string "dot"
\s+ # optional whitespace, greedily
///ig, # Modifiers: Case-insensitive; global (match multiple times)
'.'
]
]
settings = $.extend({}, @defaultOptions, options)
# Code here will run each time this plugin is invoked
@each (i, el) ->
$scope = $(el) # If you need it!
$scope.find(".#{settings.class}").each (j, anchor) ->
$anchor = $(anchor)
address = $anchor.data settings.dataKey
if address
fromText = false
else
fromText = true
address = $anchor.text()
for replacement in settings.replacements
address = address.replace replacement[0], replacement[1]
$anchor.text(address) if fromText
$anchor.attr 'href', "mailto:#{address}"
true # continue (to avoid returning anything weird)
@ # allow chaining
) this.jQuery or this.Zepto or this.$, this