custom.js
99 lines
| 1 | /* Email Encoder */ |
| 2 | /*global jQuery, window*/ |
| 3 | jQuery(function ($) { |
| 4 | |
| 5 | 'use strict'; |
| 6 | |
| 7 | // encoding method |
| 8 | function rot13(s) { |
| 9 | // source: http://jsfromhell.com/string/rot13 |
| 10 | return s.replace(/[a-zA-Z]/g, function (c) { |
| 11 | return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26); |
| 12 | }); |
| 13 | } |
| 14 | |
| 15 | // fetch email from data attribute |
| 16 | function fetchEmail(el) { |
| 17 | var email = el.getAttribute('data-enc-email'); |
| 18 | |
| 19 | if (!email) { |
| 20 | return null; |
| 21 | } |
| 22 | |
| 23 | // replace [at] sign |
| 24 | email = email.replace(/\[at\]/g, '@'); |
| 25 | |
| 26 | // encode |
| 27 | email = rot13(email); |
| 28 | |
| 29 | return email; |
| 30 | } |
| 31 | |
| 32 | // replace email in title attribute |
| 33 | function parseTitle(el) { |
| 34 | var title = el.getAttribute('title'); |
| 35 | var email = fetchEmail(el); |
| 36 | |
| 37 | if (title && email) { |
| 38 | title = title.replace('{{email}}', email); |
| 39 | el.setAttribute('title', title); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // set input value attribute |
| 44 | function setInputValue(el) { |
| 45 | var email = fetchEmail(el); |
| 46 | |
| 47 | if (email) { |
| 48 | el.setAttribute('value', email); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // open mailto link |
| 53 | function mailto(el) { |
| 54 | var email = fetchEmail(el); |
| 55 | |
| 56 | if (email) { |
| 57 | window.location.href = 'mailto:' + email; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // revert |
| 62 | function revert(el, rtl) { |
| 63 | var email = fetchEmail(el); |
| 64 | |
| 65 | if (email) { |
| 66 | rtl.text(email); |
| 67 | rtl.removeClass('eeb-rtl'); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // prepare for copying email |
| 72 | document.addEventListener('copy', function(e){ |
| 73 | $('a[data-enc-email]').each(function () { |
| 74 | var rtl = $(this).find('.eeb-rtl'); |
| 75 | |
| 76 | if (rtl.text()) { |
| 77 | revert(this, rtl); |
| 78 | } |
| 79 | }); |
| 80 | console.log('copy'); |
| 81 | }); |
| 82 | |
| 83 | // set mailto click |
| 84 | $('body').on('click', 'a[data-enc-email]', function () { |
| 85 | mailto(this); |
| 86 | }); |
| 87 | |
| 88 | // parse title attirbute |
| 89 | $('a[data-enc-email]').each(function () { |
| 90 | parseTitle(this); |
| 91 | }); |
| 92 | |
| 93 | // parse input fields |
| 94 | $('input[data-enc-email]').each(function () { |
| 95 | setInputValue(this); |
| 96 | }); |
| 97 | |
| 98 | }); |
| 99 |