-
Notifications
You must be signed in to change notification settings - Fork 22
/
jquery.urlshortener.js
92 lines (80 loc) · 2.67 KB
/
jquery.urlshortener.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*!
* jQuery URL Shortener 1.0
* https://github.com/hayageek/jQuery-URL-shortener
*
* Date: 24-Oct-2013
*/
(function ($) {
var scriptsLoaded = false;
var clientLoaded = false;
$.getScript("https://apis.google.com/js/client.js", function () {
(function checkIfLoaded() {
if (gapi.client) {
scriptsLoaded = true;
gapi.client.setApiKey($.urlShortener.settings.apiKey);
gapi.client.load('urlshortener', $.urlShortener.settings.version, function () {
clientLoaded = true;
});
} else window.setTimeout(checkIfLoaded, 10);
})();
});
$.urlShortener = function (options) {
var settings = {};
var data = {};
$.extend(settings, $.urlShortener.settings, options);
(function checkScriptsAndClientLoaded() {
if (scriptsLoaded && clientLoaded) {
if (settings.longUrl != undefined) {
longToShort(settings);
} else if (settings.shortUrl != undefined) //URL info
{
shortUrlInfo(settings);
}
} else {
window.setTimeout(checkScriptsAndClientLoaded, 10);
}
})();
function longToShort(s) {
var data = {
'longUrl': s.longUrl
};
var request = gapi.client.urlshortener.url.insert({
'resource': data
});
request.execute(function (response) {
if (response.id != null) {
if (s.success) {
s.success.call(this, response.id);
}
} else {
if (s.error) {
s.error.call(this, response.error);
}
}
});
}
function shortUrlInfo(s) {
var data = {
'shortUrl': s.shortUrl,
'projection': s.projection
};
var request = gapi.client.urlshortener.url.get(data);
request.execute(function (response) {
if (response.longUrl != null) {
if (s.success) {
if (s.projection == undefined) s.success.call(this, response.longUrl);
else s.success.call(this, response);
}
} else {
if (s.error) {
s.error.call(this, response.error);
}
}
});
}
}
$.urlShortener.settings = {
apiKey: '',
version: 'v1',
};
}(jQuery));