Nipun commited on
Commit
32912ee
·
1 Parent(s): 172457b

Fix JavaScript function scoping - make all functions globally available

Browse files
Files changed (1) hide show
  1. app.py +79 -11
app.py CHANGED
@@ -185,21 +185,80 @@ def get_video_info(video_file):
185
 
186
  # Client-side Google Drive integration
187
  google_drive_js = """
188
- <script src="https://apis.google.com/js/api.js"></script>
189
  <script>
190
- // Google Drive Picker API
191
- let pickerApiLoaded = false;
192
- let oauthToken;
193
 
194
- // Initialize the APIs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  function initializeGoogleDrive() {
196
  console.log('🚀 Initializing Google Drive...');
 
 
 
 
 
 
197
  gapi.load('auth2:picker', onAuthApiLoad);
198
  }
199
 
200
  function onAuthApiLoad() {
201
  console.log('✅ Auth API loaded');
202
- window.gapi.load('picker', onPickerApiLoad);
203
 
204
  // Initialize OAuth2
205
  gapi.auth2.init({
@@ -215,7 +274,7 @@ function onAuthApiLoad() {
215
 
216
  function onPickerApiLoad() {
217
  console.log('✅ Picker API loaded');
218
- pickerApiLoaded = true;
219
  }
220
 
221
  // Authenticate user
@@ -273,7 +332,7 @@ function openDrivePicker() {
273
  }
274
 
275
  // Handle file selection
276
- function pickerCallback(data) {
277
  if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
278
  const file = data[google.picker.Response.DOCUMENTS][0];
279
  const fileId = file[google.picker.Document.ID];
@@ -284,7 +343,7 @@ function pickerCallback(data) {
284
  // Download the file
285
  downloadFromDrive(fileId, fileName);
286
  }
287
- }
288
 
289
  // Download file from Google Drive
290
  function downloadFromDrive(fileId, fileName) {
@@ -310,8 +369,17 @@ function downloadFromDrive(fileId, fileName) {
310
  });
311
  }
312
 
313
- // Initialize when page loads
314
- window.addEventListener('load', initializeGoogleDrive);
 
 
 
 
 
 
 
 
 
315
  </script>
316
 
317
  <div id="google-drive-section" style="border: 1px solid #ddd; padding: 15px; margin: 10px 0; border-radius: 8px;">
 
185
 
186
  # Client-side Google Drive integration
187
  google_drive_js = """
188
+ <script src="https://apis.google.com/js/api.js" async defer></script>
189
  <script>
190
+ // Google Drive Picker API - make functions global
191
+ window.pickerApiLoaded = false;
192
+ window.oauthToken = null;
193
 
194
+ // Ensure functions are globally available
195
+ window.authenticateGoogleDrive = function() {
196
+ console.log('🔐 Starting authentication...');
197
+ try {
198
+ const authInstance = gapi.auth2.getAuthInstance();
199
+ if (!authInstance) {
200
+ throw new Error('Auth instance not available - please wait for APIs to load');
201
+ }
202
+
203
+ authInstance.signIn({
204
+ scope: 'https://www.googleapis.com/auth/drive.file'
205
+ }).then(() => {
206
+ window.oauthToken = authInstance.currentUser.get().getAuthResponse().access_token;
207
+ console.log('✅ Google Drive authenticated!');
208
+ document.getElementById('drive-status').innerText = '✅ Authenticated with Google Drive - ready to browse!';
209
+ }).catch(error => {
210
+ console.error('❌ Authentication failed:', error);
211
+ document.getElementById('drive-status').innerText = '❌ Authentication failed: ' + error.message;
212
+ });
213
+ } catch (error) {
214
+ console.error('❌ Auth error:', error);
215
+ document.getElementById('drive-status').innerText = '❌ Error: ' + error.message + ' - Please wait for APIs to load';
216
+ }
217
+ };
218
+
219
+ window.openDrivePicker = function() {
220
+ console.log('📁 Opening Drive picker...', { pickerApiLoaded: window.pickerApiLoaded, hasToken: !!window.oauthToken });
221
+
222
+ if (!window.pickerApiLoaded) {
223
+ document.getElementById('drive-status').innerText = '❌ Picker API not loaded yet - please wait';
224
+ return;
225
+ }
226
+
227
+ if (!window.oauthToken) {
228
+ document.getElementById('drive-status').innerText = '❌ Please authenticate first';
229
+ return;
230
+ }
231
+
232
+ try {
233
+ const picker = new google.picker.PickerBuilder()
234
+ .addView(google.picker.ViewId.DOCS_VIDEOS)
235
+ .setOAuthToken(window.oauthToken)
236
+ .setDeveloperKey('AIzaSyAOYIFpJLIFUSmNGuj1-LdtIG6X2UWJY-I')
237
+ .setCallback(window.pickerCallback)
238
+ .build();
239
+ picker.setVisible(true);
240
+ console.log('✅ Picker opened');
241
+ } catch (error) {
242
+ console.error('❌ Picker error:', error);
243
+ document.getElementById('drive-status').innerText = '❌ Picker error: ' + error.message;
244
+ }
245
+ };
246
+
247
+ // Initialize the APIs when gapi loads
248
  function initializeGoogleDrive() {
249
  console.log('🚀 Initializing Google Drive...');
250
+ if (typeof gapi === 'undefined') {
251
+ console.log('⏳ GAPI not loaded yet, retrying in 1 second...');
252
+ setTimeout(initializeGoogleDrive, 1000);
253
+ return;
254
+ }
255
+
256
  gapi.load('auth2:picker', onAuthApiLoad);
257
  }
258
 
259
  function onAuthApiLoad() {
260
  console.log('✅ Auth API loaded');
261
+ gapi.load('picker', onPickerApiLoad);
262
 
263
  // Initialize OAuth2
264
  gapi.auth2.init({
 
274
 
275
  function onPickerApiLoad() {
276
  console.log('✅ Picker API loaded');
277
+ window.pickerApiLoaded = true;
278
  }
279
 
280
  // Authenticate user
 
332
  }
333
 
334
  // Handle file selection
335
+ window.pickerCallback = function(data) {
336
  if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
337
  const file = data[google.picker.Response.DOCUMENTS][0];
338
  const fileId = file[google.picker.Document.ID];
 
343
  // Download the file
344
  downloadFromDrive(fileId, fileName);
345
  }
346
+ };
347
 
348
  // Download file from Google Drive
349
  function downloadFromDrive(fileId, fileName) {
 
369
  });
370
  }
371
 
372
+ // Initialize when page loads - multiple attempts to ensure it works
373
+ window.addEventListener('load', function() {
374
+ console.log('🚀 Page loaded, starting Google Drive initialization...');
375
+ initializeGoogleDrive();
376
+ });
377
+
378
+ // Also try after a delay in case GAPI loads slowly
379
+ setTimeout(function() {
380
+ console.log('🔄 Delayed initialization attempt...');
381
+ initializeGoogleDrive();
382
+ }, 2000);
383
  </script>
384
 
385
  <div id="google-drive-section" style="border: 1px solid #ddd; padding: 15px; margin: 10px 0; border-radius: 8px;">