Mahdi Naser Moghadasi commited on
Commit
59defa2
Β·
1 Parent(s): 70dd1cc

Fix removeChild error in tutorial script - add safety checks

Browse files

- Check if element has parentNode before removing
- Wrap remove() calls in try-catch to handle React-managed elements
- Prevents NotFoundError when React removes elements before script
- Fixes white screen issue caused by DOM manipulation errors

Files changed (1) hide show
  1. scripts/create-tutorial-video.js +247 -13
scripts/create-tutorial-video.js CHANGED
@@ -79,22 +79,38 @@ async function typeText(page, selector, text, options = {}) {
79
  // Helper function to dismiss tour and modals - RAPID, NO WAITING
80
  async function dismissTourAndModals(page) {
81
  // First, try to remove tour overlay directly via JavaScript (fastest)
 
82
  await page.evaluate(() => {
83
- // Remove tour overlay
84
  const tourOverlays = document.querySelectorAll('.fixed.inset-0, [class*="backdrop-blur"], [class*="tour-overlay"]');
85
  tourOverlays.forEach(overlay => {
86
- if (overlay.style.zIndex === '9999' || overlay.getAttribute('class')?.includes('z-[9999]') || overlay.querySelector('[class*="tour"]')) {
87
- overlay.remove();
 
 
 
 
 
 
 
88
  }
89
  });
90
 
91
- // Remove any tour-related elements
92
  const tourElements = document.querySelectorAll('[class*="tour"], [id*="tour"]');
93
  tourElements.forEach(el => {
94
  if (el.textContent?.includes('Skip') || el.textContent?.includes('Next')) {
95
  // Keep skip buttons, remove overlays
96
  } else if (el.classList.contains('fixed') || el.classList.contains('absolute')) {
97
- el.remove();
 
 
 
 
 
 
 
 
98
  }
99
  });
100
  });
@@ -154,10 +170,19 @@ async function dismissTourAndModals(page) {
154
  }
155
  }
156
 
157
- // Final cleanup - remove any remaining overlays
158
  await page.evaluate(() => {
159
  const overlays = document.querySelectorAll('.fixed.inset-0, [class*="backdrop-blur"]');
160
- overlays.forEach(overlay => overlay.remove());
 
 
 
 
 
 
 
 
 
161
  });
162
 
163
  // Minimal wait
@@ -262,6 +287,45 @@ async function createTutorialVideo() {
262
  await page.goto(CONFIG.websiteUrl, { waitUntil: 'networkidle' });
263
  await page.waitForTimeout(CONFIG.delays.long);
264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  // Inject cursor indicator after page loads
266
  await injectCursorIndicator();
267
  console.log('βœ… Cursor indicator injected');
@@ -372,13 +436,168 @@ async function createTutorialVideo() {
372
  console.log('βœ… Clicked Start Recommended Problem and navigated to practice page');
373
  }
374
  } else {
375
- console.log('⚠️ Start Recommended Problem button not found');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
  }
377
 
378
  // ============================================
379
  // STEP 2: Practice Page - Dismiss tour rapidly and wait for it to be gone
380
  // ============================================
381
  console.log('πŸ“ Step 2: On Practice Page, dismissing tour...');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
382
  await dismissTourAndModals(page);
383
 
384
  // Wait for tour overlay to be completely removed
@@ -387,12 +606,18 @@ async function createTutorialVideo() {
387
  return !tourOverlay || tourOverlay.style.display === 'none' || !tourOverlay.offsetParent;
388
  }, { timeout: 5000 }).catch(() => {});
389
 
390
- // Also try to remove any remaining overlays
391
  await page.evaluate(() => {
392
  const overlays = document.querySelectorAll('.fixed.inset-0, [class*="backdrop-blur"]');
393
  overlays.forEach(overlay => {
394
- if (overlay.style.zIndex === '9999' || overlay.getAttribute('class')?.includes('z-[9999]')) {
395
- overlay.remove();
 
 
 
 
 
 
396
  }
397
  });
398
  });
@@ -461,10 +686,19 @@ async function createTutorialVideo() {
461
  // ============================================
462
  console.log('▢️ Step 4: Clicking Run button...');
463
 
464
- // Make absolutely sure tour is gone before clicking
465
  await page.evaluate(() => {
466
  const overlays = document.querySelectorAll('.fixed.inset-0, [class*="backdrop-blur"]');
467
- overlays.forEach(overlay => overlay.remove());
 
 
 
 
 
 
 
 
 
468
  });
469
  await page.waitForTimeout(100);
470
 
 
79
  // Helper function to dismiss tour and modals - RAPID, NO WAITING
80
  async function dismissTourAndModals(page) {
81
  // First, try to remove tour overlay directly via JavaScript (fastest)
82
+ // But be careful not to remove elements that React is managing
83
  await page.evaluate(() => {
84
+ // Remove tour overlay - check if element still has a parent before removing
85
  const tourOverlays = document.querySelectorAll('.fixed.inset-0, [class*="backdrop-blur"], [class*="tour-overlay"]');
86
  tourOverlays.forEach(overlay => {
87
+ // Only remove if it's still in the DOM and matches our criteria
88
+ if (overlay.parentNode &&
89
+ (overlay.style.zIndex === '9999' || overlay.getAttribute('class')?.includes('z-[9999]') || overlay.querySelector('[class*="tour"]'))) {
90
+ try {
91
+ overlay.remove();
92
+ } catch (e) {
93
+ // Element might already be removed by React, ignore error
94
+ console.log('Element already removed:', e);
95
+ }
96
  }
97
  });
98
 
99
+ // Remove any tour-related elements - be more careful
100
  const tourElements = document.querySelectorAll('[class*="tour"], [id*="tour"]');
101
  tourElements.forEach(el => {
102
  if (el.textContent?.includes('Skip') || el.textContent?.includes('Next')) {
103
  // Keep skip buttons, remove overlays
104
  } else if (el.classList.contains('fixed') || el.classList.contains('absolute')) {
105
+ // Only remove if still in DOM
106
+ if (el.parentNode) {
107
+ try {
108
+ el.remove();
109
+ } catch (e) {
110
+ // Element might already be removed by React, ignore error
111
+ console.log('Element already removed:', e);
112
+ }
113
+ }
114
  }
115
  });
116
  });
 
170
  }
171
  }
172
 
173
+ // Final cleanup - remove any remaining overlays (safely)
174
  await page.evaluate(() => {
175
  const overlays = document.querySelectorAll('.fixed.inset-0, [class*="backdrop-blur"]');
176
+ overlays.forEach(overlay => {
177
+ // Only remove if still in DOM
178
+ if (overlay.parentNode) {
179
+ try {
180
+ overlay.remove();
181
+ } catch (e) {
182
+ // Element might already be removed by React, ignore error
183
+ }
184
+ }
185
+ });
186
  });
187
 
188
  // Minimal wait
 
287
  await page.goto(CONFIG.websiteUrl, { waitUntil: 'networkidle' });
288
  await page.waitForTimeout(CONFIG.delays.long);
289
 
290
+ // Wait for Next.js to fully hydrate - check for actual content
291
+ console.log('⏳ Waiting for page to fully load...');
292
+ try {
293
+ await page.waitForFunction(() => {
294
+ // Check if Next.js has finished loading by looking for actual content
295
+ const body = document.body;
296
+ if (!body) return false;
297
+
298
+ // Check for buttons or other interactive elements
299
+ const hasButtons = body.querySelectorAll('button').length > 0;
300
+ const hasContent = body.textContent && body.textContent.length > 100;
301
+ // Check that we're not still in hydration (Next.js loading state)
302
+ const isNotLoading = !body.textContent?.includes('self.__next_f') ||
303
+ (body.textContent.length > 500 && !body.textContent.startsWith('(self.__next_f'));
304
+
305
+ return hasButtons && hasContent && isNotLoading;
306
+ }, { timeout: 15000 });
307
+ console.log('βœ… Page fully loaded');
308
+ } catch (e) {
309
+ console.log('⚠️ Page load timeout, checking current state...');
310
+ const pageState = await page.evaluate(() => {
311
+ return {
312
+ url: window.location.href,
313
+ title: document.title,
314
+ bodyLength: document.body?.textContent?.length || 0,
315
+ buttonCount: document.querySelectorAll('button').length,
316
+ hasNextJsLoading: document.body?.textContent?.includes('self.__next_f') || false
317
+ };
318
+ });
319
+ console.log('Page state:', pageState);
320
+
321
+ if (pageState.hasNextJsLoading || pageState.buttonCount === 0) {
322
+ console.log('⚠️ Page still loading, waiting longer...');
323
+ await page.waitForTimeout(5000);
324
+ }
325
+ }
326
+
327
+ await page.waitForTimeout(2000); // Additional wait for stability
328
+
329
  // Inject cursor indicator after page loads
330
  await injectCursorIndicator();
331
  console.log('βœ… Cursor indicator injected');
 
436
  console.log('βœ… Clicked Start Recommended Problem and navigated to practice page');
437
  }
438
  } else {
439
+ console.log('⚠️ Start Recommended Problem button not found, trying manual navigation...');
440
+
441
+ // Check if we're already on practice page
442
+ const isAlreadyOnPracticePage = await page.evaluate(() => {
443
+ const hasEditor = !!document.querySelector('#tour-code-editor, .monaco-editor, .cm-editor');
444
+ // Check for run button using standard DOM methods
445
+ const runButton = document.querySelector('#tour-run-button') ||
446
+ Array.from(document.querySelectorAll('button')).find(btn => btn.textContent?.includes('Run'));
447
+ const hasRun = !!runButton;
448
+ return hasEditor && hasRun;
449
+ });
450
+
451
+ if (!isAlreadyOnPracticePage) {
452
+ console.log('πŸ“ Not on practice page, navigating manually...');
453
+
454
+ // Try to select a topic (first available)
455
+ const topicButton = page.locator('#tour-topic-selection button, .grid button').first();
456
+ if (await topicButton.isVisible({ timeout: 3000 }).catch(() => false)) {
457
+ const topicBox = await topicButton.boundingBox();
458
+ if (topicBox) {
459
+ await moveHandToTarget(page, topicBox.x + topicBox.width / 2, topicBox.y + topicBox.height / 2);
460
+ await topicButton.click();
461
+ // Wait for UI to update after topic selection
462
+ await page.waitForTimeout(1000);
463
+ // Wait for page to be interactive again
464
+ await page.waitForLoadState('networkidle', { timeout: 5000 }).catch(() => {});
465
+ console.log('βœ… Selected topic');
466
+ }
467
+ }
468
+
469
+ // Try to select difficulty (Easy)
470
+ const difficultyButton = page.locator('#tour-difficulty-selection button:has-text("Easy"), .flex button:has-text("Easy")').first();
471
+ if (await difficultyButton.isVisible({ timeout: 3000 }).catch(() => false)) {
472
+ const diffBox = await difficultyButton.boundingBox();
473
+ if (diffBox) {
474
+ await moveHandToTarget(page, diffBox.x + diffBox.width / 2, diffBox.y + diffBox.height / 2);
475
+ await difficultyButton.click();
476
+ // Wait for UI to update after difficulty selection
477
+ await page.waitForTimeout(1000);
478
+ // Wait for page to be interactive again
479
+ await page.waitForLoadState('networkidle', { timeout: 5000 }).catch(() => {});
480
+ console.log('βœ… Selected difficulty');
481
+ }
482
+ }
483
+
484
+ // Click Start Practicing button
485
+ const startPracticingButton = page.locator('#tour-start-button, button:has-text("Start Practicing")').first();
486
+ if (await startPracticingButton.isVisible({ timeout: 3000 }).catch(() => false)) {
487
+ const startBox = await startPracticingButton.boundingBox();
488
+ if (startBox) {
489
+ await moveHandToTarget(page, startBox.x + startBox.width / 2, startBox.y + startBox.height / 2);
490
+ await startPracticingButton.click();
491
+ console.log('βœ… Clicked Start Practicing');
492
+
493
+ // Wait for navigation to start
494
+ await page.waitForTimeout(1000);
495
+
496
+ // Wait for navigation to start (URL might change or page might reload)
497
+ await page.waitForTimeout(2000);
498
+
499
+ // Wait for practice page to load - check for actual content
500
+ console.log('⏳ Waiting for practice page to load...');
501
+ try {
502
+ // First wait for page to not be in loading state
503
+ await page.waitForFunction(() => {
504
+ const body = document.body;
505
+ if (!body) return false;
506
+ // Check that we're past the Next.js hydration
507
+ const isNotLoading = body.textContent &&
508
+ body.textContent.length > 500 &&
509
+ !body.textContent.startsWith('(self.__next_f');
510
+ return isNotLoading;
511
+ }, { timeout: 10000 });
512
+
513
+ // Then wait for practice page elements
514
+ await page.waitForFunction(() => {
515
+ const hasEditor = !!document.querySelector('#tour-code-editor, .monaco-editor, .cm-editor');
516
+ const runButton = document.querySelector('#tour-run-button') ||
517
+ Array.from(document.querySelectorAll('button')).find(btn => btn.textContent?.includes('Run'));
518
+ const hasRun = !!runButton;
519
+ return hasEditor && hasRun;
520
+ }, { timeout: 10000 });
521
+
522
+ console.log('βœ… Practice page loaded successfully');
523
+ } catch (e) {
524
+ console.log('⚠️ Practice page load timeout, checking current state...');
525
+ const debugState = await page.evaluate(() => {
526
+ return {
527
+ url: window.location.href,
528
+ bodyLength: document.body?.textContent?.length || 0,
529
+ isHydrating: document.body?.textContent?.startsWith('(self.__next_f') || false,
530
+ hasEditor: !!document.querySelector('#tour-code-editor, .monaco-editor, .cm-editor'),
531
+ buttonCount: document.querySelectorAll('button').length
532
+ };
533
+ });
534
+ console.log('Debug state:', debugState);
535
+ }
536
+
537
+ // Additional wait for stability
538
+ await page.waitForTimeout(2000);
539
+ await page.waitForLoadState('networkidle', { timeout: 5000 }).catch(() => {});
540
+ await injectCursorIndicator();
541
+ }
542
+ }
543
+ } else {
544
+ console.log('βœ… Already on practice page');
545
+ }
546
  }
547
 
548
  // ============================================
549
  // STEP 2: Practice Page - Dismiss tour rapidly and wait for it to be gone
550
  // ============================================
551
  console.log('πŸ“ Step 2: On Practice Page, dismissing tour...');
552
+
553
+ // Verify we're actually on the practice page before continuing
554
+ const verifyPracticePage = await page.evaluate(() => {
555
+ const hasEditor = !!document.querySelector('#tour-code-editor, .monaco-editor, .cm-editor');
556
+ // Check for run button using standard DOM methods
557
+ const runButton = document.querySelector('#tour-run-button') ||
558
+ Array.from(document.querySelectorAll('button')).find(btn => btn.textContent?.includes('Run'));
559
+ const hasRun = !!runButton;
560
+ return hasEditor && hasRun;
561
+ });
562
+
563
+ if (!verifyPracticePage) {
564
+ console.log('❌ Not on practice page! Waiting for page to load...');
565
+ await page.waitForTimeout(3000);
566
+
567
+ // Try one more time
568
+ const retryVerify = await page.evaluate(() => {
569
+ const hasEditor = !!document.querySelector('#tour-code-editor, .monaco-editor, .cm-editor');
570
+ // Check for run button using standard DOM methods
571
+ const runButton = document.querySelector('#tour-run-button') ||
572
+ Array.from(document.querySelectorAll('button')).find(btn => btn.textContent?.includes('Run'));
573
+ const hasRun = !!runButton;
574
+ return hasEditor && hasRun;
575
+ });
576
+
577
+ if (!retryVerify) {
578
+ console.log('❌ Still not on practice page! Taking screenshot for debugging...');
579
+ await page.screenshot({ path: 'debug-white-page.png' });
580
+ console.log('πŸ“Έ Screenshot saved to debug-white-page.png');
581
+
582
+ // Check what's actually on the page
583
+ const pageState = await page.evaluate(() => {
584
+ return {
585
+ url: window.location.href,
586
+ title: document.title,
587
+ bodyText: document.body?.textContent?.substring(0, 200) || 'empty',
588
+ hasEditor: !!document.querySelector('#tour-code-editor, .monaco-editor, .cm-editor'),
589
+ hasRun: !!Array.from(document.querySelectorAll('button')).find(btn => btn.textContent?.includes('Run')),
590
+ hasSelector: !!Array.from(document.querySelectorAll('button')).find(btn => btn.textContent?.includes('Start Practicing')),
591
+ allButtons: Array.from(document.querySelectorAll('button')).map(btn => btn.textContent?.trim()).filter(Boolean).slice(0, 10)
592
+ };
593
+ });
594
+ console.log('Current page state:', JSON.stringify(pageState, null, 2));
595
+
596
+ // Instead of throwing, try to continue - maybe we can still work with what we have
597
+ console.log('⚠️ Continuing anyway - will try to proceed with current page state');
598
+ }
599
+ }
600
+
601
  await dismissTourAndModals(page);
602
 
603
  // Wait for tour overlay to be completely removed
 
606
  return !tourOverlay || tourOverlay.style.display === 'none' || !tourOverlay.offsetParent;
607
  }, { timeout: 5000 }).catch(() => {});
608
 
609
+ // Also try to remove any remaining overlays (safely)
610
  await page.evaluate(() => {
611
  const overlays = document.querySelectorAll('.fixed.inset-0, [class*="backdrop-blur"]');
612
  overlays.forEach(overlay => {
613
+ // Only remove if still in DOM and matches criteria
614
+ if (overlay.parentNode &&
615
+ (overlay.style.zIndex === '9999' || overlay.getAttribute('class')?.includes('z-[9999]'))) {
616
+ try {
617
+ overlay.remove();
618
+ } catch (e) {
619
+ // Element might already be removed by React, ignore error
620
+ }
621
  }
622
  });
623
  });
 
686
  // ============================================
687
  console.log('▢️ Step 4: Clicking Run button...');
688
 
689
+ // Make absolutely sure tour is gone before clicking (safely)
690
  await page.evaluate(() => {
691
  const overlays = document.querySelectorAll('.fixed.inset-0, [class*="backdrop-blur"]');
692
+ overlays.forEach(overlay => {
693
+ // Only remove if still in DOM
694
+ if (overlay.parentNode) {
695
+ try {
696
+ overlay.remove();
697
+ } catch (e) {
698
+ // Element might already be removed by React, ignore error
699
+ }
700
+ }
701
+ });
702
  });
703
  await page.waitForTimeout(100);
704