PageRenderTime 38ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/jquery.email-address-de-obfuscator.js.coffee

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