-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdrive.js
156 lines (149 loc) · 5.3 KB
/
gdrive.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
$(function() {
// Sets the copyright year
var date = new Date();
$('#year').text(date.getFullYear());
// Creates a clipboard variable
var clipboard = new Clipboard('#copy-btn');
// Clears all tooltips when a mouse out event is emitted
$('#copy-btn').mouseout(function() {
$('.material-tooltip').remove();
});
// Listener for successful copy
clipboard.on('success', function(e) {
// Sets a tooltip for the button
$('#copy-btn').tooltip({ delay: 0, tooltip: 'Copied!' });
// Workaround to make the tooltip appear
$('#copy-btn').mouseover();
// Clears the tooltip
setTimeout(function() {
// $('#copy-btn').tooltip('remove');
}, 1000);
e.clearSelection();
});
// Listener for error while trying to copy to the clipboard
clipboard.on('error', function(e) {
// Sets a tooltip for the button
var action = /Mac/i.test(navigator.userAgent) ? '⌘' : 'Ctrl';
$('#copy-btn').tooltip({
delay: 0,
tooltip: `Press ${action}-C to Copy`,
});
// Workaround to make the tooltip appear
$('#copy-btn').mouseover();
// Clears the tooltip
setTimeout(function() {
// $('#copy-btn').tooltip('remove');
}, 1000);
});
var apiKey = 'AIzaSyBKfj9ljf-ECV4rVGrqIBTe0O8p7bFgG7U';
var regex = /[A-z_\-0-9]{28}/;
var idArray = [];
var idIndex = 0;
var maxLines = 20;
// Listens on keydown in order to prevent linebreaks more than the maxLines variable
$('#url').on('keydown', function(e) {
if (
e.keyCode == 13 &&
$(this)
.val()
.split('\n').length >= maxLines
) {
return false;
}
});
// Shows max lines text
$('#max-lines').text(maxLines);
// Disables or enables the buttons
function toggleDisabled() {
$('#progress').toggleClass('hide');
toggleElementDisabled('#url');
toggleElementDisabled('#submit');
toggleElementDisabled('#copy-btn');
}
// Toggles the disabled attribute
function toggleElementDisabled(selector) {
if ($('#progress.hide')[0]) {
$(selector).removeAttr('disabled');
} else {
$(selector).attr('disabled', 'disabled');
}
}
// Makes the Google Drive API call.
function makeApiCall() {
if (idIndex < idArray.length) {
// Checks if is the last call
var id = idArray[idIndex];
id = id.split('/')[0];
if (id) {
$.ajax({
url: `https://www.googleapis.com/drive/v3/files/${id}?key=${apiKey}&fields=webContentLink`,
})
.then(function(data) {
if (data.webContentLink) {
var formattedWebContentLink = data.webContentLink.replace(
/&export=download/,
''
);
$('#data').append(
`<li><a href="${formattedWebContentLink}">${formattedWebContentLink}</a></li>`
);
} else {
$('#data').append(
`<li>Could not get direct link for: ${idArray[idIndex]}`
);
}
idIndex++;
setTimeout(function() {
makeApiCall();
}, 500);
})
.fail(function(e, textStatus) {
$('#data').append(`<li>${e.responseJSON.error.message}</li>`);
idIndex++;
setTimeout(function() {
makeApiCall();
}, 500);
});
} else {
idIndex++;
setTimeout(function() {
makeApiCall();
}, 500);
}
} else {
// Ends recursive calling
toggleDisabled();
}
}
// Submit listener
$('#direct-link-form').on('submit', function(e) {
e.preventDefault();
idIndex = 0;
idArray = $('#url')
.val()
.split('\n')
.slice(0, maxLines)
.join('\n')
.split(
/https:\/\/drive\.google\.com\/open\?id=|https:\/\/drive\.google\.com\/file\/d\//
)
.slice(1);
// Checks for every id if it has a correct format.
if (
idArray.length > 0 &&
idArray.every(function(id) {
return regex.test(id);
})
) {
$('#data').text('');
$('#result').removeClass('hide');
idArray.map(function(parsedId) {
return parsedId.split('/')[0];
});
toggleDisabled();
makeApiCall();
} else {
alert('Unable to get the ID from the URL(s).');
}
});
});