Skip to content

fix: improve screenshot reliability with page readiness check#4

Open
RapierCraft wants to merge 1 commit intomainfrom
fix/screenshot-empty-data
Open

fix: improve screenshot reliability with page readiness check#4
RapierCraft wants to merge 1 commit intomainfrom
fix/screenshot-empty-data

Conversation

@RapierCraft
Copy link
Copy Markdown
Owner

Summary

  • Improves comet_screenshot reliability by waiting for page to be ready before capturing
  • Adds proper error handling and validation for empty screenshot data

Problem

Users reported that comet_screenshot returns empty content without any error message (#2).

Root Cause Analysis

The original implementation had several issues:

  1. No connection resilience (withAutoReconnect wrapper missing)
  2. No page readiness check - screenshot could be captured before content rendered
  3. No validation of returned data - empty results passed silently
  4. No error context when failures occurred

Solution

async screenshot(format: "png" | "jpeg" = "png"): Promise<ScreenshotResult> {
  return this.withAutoReconnect(async () => {
    this.ensureConnected();

    // Wait for page to be ready before capturing
    try {
      await this.client!.Runtime.evaluate({
        expression: `
          new Promise((resolve) => {
            if (document.readyState === 'complete') {
              resolve(true);
            } else {
              window.addEventListener('load', () => resolve(true), { once: true });
              setTimeout(() => resolve(true), 3000); // Fallback timeout
            }
          })
        `,
        awaitPromise: true,
        timeout: 5000,
      });
    } catch {
      // Continue anyway - page might still be usable
    }

    // Small delay to ensure rendering is complete
    await new Promise(resolve => setTimeout(resolve, 100));

    const result = await this.client!.Page.captureScreenshot({
      format,
      captureBeyondViewport: false,
    });

    if (!result || !result.data) {
      throw new Error(
        "Screenshot returned empty data. Ensure you're connected to a visible tab with content."
      );
    }

    return { data: result.data };
  });
}

Changes

  • Added withAutoReconnect wrapper for connection resilience
  • Added page readiness check (waits for document.readyState === 'complete')
  • Added 100ms delay after load to ensure rendering completes
  • Added captureBeyondViewport: false for consistent behavior
  • Added validation with descriptive error message if data is empty

Test Plan

  • Test screenshot on Perplexity main tab
  • Test screenshot immediately after navigation
  • Test screenshot when connection drops mid-operation
  • Verify error message appears when screenshot fails

Fixes #2

- Wait for document.readyState === 'complete' before capturing
- Add small delay after load to ensure rendering is complete
- Validate result data and throw descriptive error if empty
- Add captureBeyondViewport: false for consistent behavior

Tested locally:
- Screenshot captured successfully (55KB, 202ms)
- Page readiness check works correctly
- Empty data validation working

Fixes #2
@RapierCraft RapierCraft force-pushed the fix/screenshot-empty-data branch from 96f5a0b to fc92f4b Compare January 25, 2026 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

comet_screenshot tool not working - CDP captureScreenshot type mismatch

1 participant