-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitlr.js
66 lines (47 loc) · 1.49 KB
/
bitlr.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
/*!
Name: Bitlr
Dependencies: jQuery
Author: Michael Lynch
Author URL: http://michaelynch.com
Date Created: May 27, 2014
Last Updated: May 27, 2014
Licensed under the MIT license
*/
;(function($) {
$.bitlr = function(options) {
//define default parameters
var defaults = {
error: function(message) {},
success: function() {}
}
//define plugin
var plugin = this;
//define settings
plugin.settings = {}
//merge defaults and options
plugin.settings = $.extend({}, defaults, options);
//namespace settings
var s = plugin.settings;
//encode link
s.link = encodeURI(s.link);
$.ajax({
type: 'GET',
url: 'https://api-ssl.bitly.com/v3/shorten?access_token='+s.apiKey+'&longUrl='+s.link,
dataType: 'json',
success: function(result) {
//if anchor is true
if(s.anchor === true) {
//run success callback with new link as an anchor
s.success.call(this, '<a href="'+result.data.url+'">'+result.data.url+'</a>');
} else {
//run success callback with raw new link
s.success.call(this, result.data.url);
}
},
error: function(jqXHR, textStatus, errorThrown) {
//run error callback
s.error.call(this);
}
});
}
})(jQuery);