forked from hayageek/jQuery-URL-shortener
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.urlshortener.js
More file actions
100 lines (82 loc) · 1.82 KB
/
Copy pathjquery.urlshortener.js
File metadata and controls
100 lines (82 loc) · 1.82 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*!
* jQuery URL Shortener v@VERSION
* https://github.com/hayageek/jQuery-URL-shortener
*
*
* Date: @DATE
*/
(function($) {
$.urlShortener = function(options)
{
var settings ={};
$.extend(settings,$.urlShortener.settings, options);
var requestUrl = settings.requestUrl;
if(settings.apiKey.length > 1)
{
requestUrl += "key="+settings.apiKey;
}
if(settings.longUrl != undefined)
{
var data = {longUrl: settings.longUrl};
var shortUrl=undefined;
return $.urlShortener.shortUrl(requestUrl,data);
}
else if(settings.shortUrl != undefined) //URL info
{
requestUrl += "&shortUrl="+settings.shortUrl;
return $.urlShortener.urlInfo(requestUrl,settings.projection);
}
return undefined;
};
$.urlShortener.shortUrl = function(requestUrl,data)
{
var shortUrl =undefined;
$.ajax({
async:false,
type: "POST",
url: requestUrl,
data: JSON.stringify(data),
contentType:"application/json; charset=utf-8",
dataType:"json",
}).done(function( info )
{
shortUrl=info.id;
}).fail(function(jqXHR, textStatus, errorThrown)
{
});
return shortUrl;
}
$.urlShortener.urlInfo = function(requestUrl,projection)
{
if(projection != undefined)
{
requestUrl += "&projection="+projection;
}
var urlInfo =undefined;
$.ajax({
async:false,
type: "GET",
url: requestUrl,
contentType:"application/json; charset=utf-8",
dataType:"json",
}).done(function( info )
{
if(projection == undefined)
{
urlInfo=info.longUrl; //return long URL;
}
else
{
urlInfo = info; //return full info;
}
}).fail(function(jqXHR, textStatus, errorThrown)
{
});
return urlInfo;
}
$.urlShortener.settings = {
apiKey : '',
version : 'v1',
requestUrl : 'https://www.googleapis.com/urlshortener/v1/url?'
};
}(jQuery));