jQuery URL shortener

Hi y’all,

My first jQuery plugin. Your welcome to use it (including commerical), re-code it and provide suggestions, please just keep reference to myself and to Uzbekjon.

It uses the http://bit.ly API so you’ll need your own account and API key. The code format is based on Mike Alsup’s Plugin Development Pattern.

Download/fork from Github: https://github.com/Kwozzie/jquery-url-shortener

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
/*
* jQuery Bit.Ly Shorten Url Plugin
* 2010 Justin Swan
* License: Creative Commons Attribution-Share Alike 3.0 Australia Licence
* Original Author: Uzbekjon (http://jquery-howto.blogspot.com/2009/04/shorten-long-urls-with-jquery-bitly.html)
* Version: 1.0.0
*
* Grateful for any feedback on improvements.
*
* Call: $(input field).shorten_url(long url)
* Returns: shortened url as value for input field.
*
*/
 
//
// start closure
//
(function($){
$.fn.shorten_url = function(url,options){
// Extend our default options with those provided.
var opts = $.extend($.fn.shorten_url.defaults, options);
 
return this.each(function(){
$this = $(this);
 
// format url to request
var daurl = "http://api.bit.ly/shorten?"
+"version="+opts.version
+"&longUrl="+url
+"&login="+opts.login
+"&apiKey="+opts.apiKey
+"&history="+opts.history
+"&format=json&callback=?";
 
// Utilize the bit.ly API
$.getJSON(daurl, function(data){
// Make a good use of short URL
$this.val(data.results[url].shortUrl);
});
});
};
 
$.fn.shorten_url.defaults = {
version: '2.0.1',
login: 'bitlyapidemo', // your bit.ly account login
apiKey: 'R_0da49e0a9118ff35f52f629d2d71bf07', // your api key here
history: '0',
};
//
// end of closure
//
})(jQuery);

 

jQuery email obfuscation

Was looking for a way to hide or obfuscate email addresses from spam bots and found this Google groups page which was pretty much what I was after for a solution, however this plugin isn’t quite complete. Javascripts .replace only works once, normally, so an email address with multiple ‘ DOT ‘ s will not deobfuscate correctly.

A quick Google later… here’s my solution

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
jQuery.fn.deobfuscate = function()
{
return this.each(function()
{
var content = $(this).text();</code>
 
/* grab the part inside the braces, swap out placeholders, and trim
*/
var obfuscated = content.match(/\[(.*)\]/);
/*
for(var i=0;i<obfuscated.length();i++){
alert(obfuscated[i]);
}
*/
var address = obfuscated[1]
.replace(' AT ', '@')
.replace(new RegExp(' DOT ', 'g'), '.')
.replace(/^\s+|\s+$/g, '');
 
/* get everything before the braces and trim
*/
var text = content.match(/.?[^[]+/);
 
text = (text[0] != content)
? text[0].replace(/^\s+|\s+$/g, '')
: address;      // if there's no text part, use the address
 
var title = $(this).attr('title') || '';
 
$(this).replaceWith($('<a href="mailto:' + address + '" title="' +
title + '">' + text + '</a>'));
});
 
};

note the second .replace now contains a new Regex() command which looks for all occurrances of ‘ DOT ‘. Works a treat.