-
Notifications
You must be signed in to change notification settings - Fork 0
/
link-shortener.js
42 lines (36 loc) · 1.27 KB
/
link-shortener.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
'use strict';
$(function() {
function getUrlVar(key) {
var result = new RegExp(key + "=([^&]*)", "i").exec(window.location.search);
return result && unescape(result[1]) || "";
}
var android = getUrlVar('android');
var ios = getUrlVar('ios');
if (android !== '' && navigator.userAgent.match(/(Android)/)) {
window.location.href = android;
}
if (ios !== '' && navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
window.location.href = ios;
}
$('#shorten').on('click', function() {
var url1 = $('#field1').val();
var url2 = $('#field2').val();
var longUrl;
var shortUrl;
if (url1 === '' || url2 === '') {
$('#shortLink').html('Please enter appstore links');
} else {
longUrl = 'http://manuelroth.me/route/?' + 'ios=' + url1 + '&android=' + url2;
var request = gapi.client.urlshortener.url.insert({
'resource': {
'longUrl': longUrl
}
});
request.then(function(response) {
$('#shortLink').html(response.result.id);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
}
});
});