forked from cowboy/jquery-misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ba-emailSpammerize.js
77 lines (64 loc) · 2.31 KB
/
jquery.ba-emailSpammerize.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*!
* emailSpammerize - v1.1 - 3/14/2009
* http://benalman.com/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Licensed under the MIT license
* http://benalman.com/about/license/
*/
(function($) {
'$:nomunge'; // Used by YUI compressor.
var str_emailSpammerize = 'emailSpammerize',
replaces = {
at: '@', dot: '.', plus: '+', dash: '-', underscore: '_', bang: '!',
hash: '#', dollar: '$', percent: '%', ampersand: '&', quote: "'",
asterisk: '*', slash: '/', equals: '=', question: '?', caret: '^',
backtick: '`', pipe: '|', tilde: '~'
};
$[ str_emailSpammerize ] = function( email, linkify ) {
var re;
$.each( replaces, function(k,v){
re = new RegExp( '\\s+' + k + '\\s+', 'gi' );
email = email.replace( re, v );
});
email = email.replace( /(\s+|\[|\])/g, '' );
email = $.map( email, function(v){
return '&#' + v.charCodeAt() + ';';
}).join('');
if ( linkify ) {
email = '<a href="mailto:' + email + '">' + email + '<\/a>';
}
return email;
};
$.fn[ str_emailSpammerize ] = function( linkify ) {
return this.each(function(){
var email = $(this).text();
$(this).html( $[ str_emailSpammerize ]( email, linkify ) );
});
};
})(jQuery);
// About:
//
// "With a little help from Randal Schwartz, who held my nose to the web standards"
//
// Convert your anti-spammerized links (like "foo at bar dot com") to their
// proper format, which spammers theoretically shouldn't be able to harvest
// since this all happens via JavaScript after page load, and all text is
// encoded into numeric entities. If an email address contains a part that is
// also the name of a replace, like "dot" or "at", just surround it with [].
//
// Sample Usage:
//
// Initial HTML:
// <span>foo ampersand bar at example dot com</span>
//
// $('span').emailSpammerize(true);
//
// Resulting HTML:
// <span><a href="mailto:foo&bar@e
// xample.com">fo
// o&bar@example
// .com</a></span>
//
// Which renders just like this would, were you to code it by hand:
// <span><a href="mailto:foo&[email protected]">foo&[email protected]</a></span>