-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGooglePicker.js
More file actions
107 lines (98 loc) · 3.77 KB
/
Copy pathGooglePicker.js
File metadata and controls
107 lines (98 loc) · 3.77 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
101
102
103
104
105
106
107
// The Browser API key obtained from the Google API Console.
const developerKey = 'mmGchmTw9KWU2HjteTZdvVih';
// The Client ID obtained from the Google API Console. Replace with your own Client ID.
const clientId = '898962898453-89d9a4dt2d5rtgf8kf5uir1pqgaihr0o.apps.googleusercontent.com';
// Scope to use to access user's google drive files.
const scope = 'https://www.googleapis.com/auth/drive.file';
let pickerApiLoaded = false;
let oauthToken;
// Function to copy the future url on the clipboard
const copyToClipboard = str => {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
};
// Use the API Loader script to load google.picker and gapi.auth.
function onApiLoad() {
gapi.load('auth2', onAuthApiLoad);
gapi.load('picker', onPickerApiLoad);
}
// When the login button is pressed sign in
function onAuthApiLoad() {
document.querySelector('.google-btn').addEventListener('click', () => {
gapi.auth2.init({ client_id: clientId }).then(function(googleAuth) {
googleAuth.signIn({ scope: scope }).then(function(result) {
handleAuthResult(result.getAuthResponse());
})
});
})
}
// Callback function to load picker
function onPickerApiLoad() {
pickerApiLoaded = true;
createPicker();
}
// On auth success create a picker
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
oauthToken = authResult.access_token;
createPicker();
}
}
// Create and render a Picker object for picking from Google drive.
function createPicker() {
if (pickerApiLoaded && oauthToken) {
const view = new google.picker.DocsView();
view.setMode(google.picker.DocsViewMode.LIST);
view.setIncludeFolders(true);
const picker = new google.picker.PickerBuilder()
.setOAuthToken(oauthToken)
.setLocale('en')
.addViewGroup(view)
.hideTitleBar()
.setSize(1920, 3280)
.setTitle('Google drive downloader by BeToBe')
.setDeveloperKey(developerKey).
setCallback(pickerCallback).
build();
picker.setVisible(true);
}
}
// Callback function that will parse the google drive download URL & copy to clipboard
function pickerCallback(data) {
let id = 'nothing';
if (data[google.picker.Response.ACTION] === google.picker.Action.PICKED) {
const doc = data[google.picker.Response.DOCUMENTS][0];
id = doc[google.picker.Document.ID];
const message = `https://cors-anywhere.herokuapp.com/https://drive.google.com/uc?id=${id}&authuser=2&export=download`;
const response = axios.post(message, '', {
headers: {
'X-Drive-First-Party': 'DriveWebUi',
'x-requested-with': 'BeTobe'
}
});
response.then(({data}) => {
const downloadUrl = data.match(/downloadUrl":"(.*?)"/);
let fileName = data.match(/fileName":"(.*?)"/);
if (fileName.length > 0) {
fileName = fileName[1];
} else {
fileName = 'Download'
}
const replaced = downloadUrl[1].replace('/\\u003d|\\u0026\g', '');
copyToClipboard(replaced);
document.getElementById('text-container').style.display = 'block';
if (fileName.length > 35) {
fileName = `${fileName.slice(0, 35)}...`
}
document.getElementById('text-container').textContent = fileName;
document.getElementById('text-container').href = replaced;
});
}
}