The Read-Aloud extension has been enhanced to support reading text content from iframes on a webpage. This provides a seamless reading experience that includes content from embedded frames, such as documentation sites, educational platforms, and news sites with embedded widgets.
- Enhanced content script injection: Added
allFrames: truetobrapi.scripting.executeScript()calls - Frame-aware injection: Modified
injectContentScript()andcontentScriptAlreadyInjected()functions to work with all frames when no specific frameId is provided
- Frame detection: Added
isMainFramedetection usingwindow.self === window.top - Frame coordination: Implemented main frame coordinator that manages communication with child frames
- Message handling: Added postMessage-based communication between main frame and iframes
- Text aggregation: Enhanced
getTexts()to collect and combine text from all accessible frames
initMainFrameCoordinator(): Initializes coordination in the main frameinitChildFrame(): Sets up message listeners in iframe contextsgetTextsWithFrames(): Aggregates text from main frame and all accessible iframesgetTextsFromFrame(): Communicates with specific iframe to retrieve texthandleFrameRequest(): Handles incoming requests from parent frame
- Frame messaging utility: Added
FrameMessagingPeerclass for iframe communication - Cross-frame support: Provides structured communication between frames
- No changes needed: The existing text extraction logic works within each frame
- Frame-agnostic: Each frame runs its own instance of the text extraction logic
- When content scripts are injected, they detect if they're running in the main frame or an iframe
- Main frame initializes as coordinator, iframes initialize as child frames
- Each frame sets up appropriate message listeners
- When reading begins, the main frame's
getTexts()function is called - Main frame extracts its own text using the existing
readAloudDoc.getTexts() - Main frame discovers all
<iframe>elements on the page - For each iframe, main frame sends a
getFrameTextsrequest via postMessage - Each accessible iframe responds with its extracted text
- Main frame aggregates all text results in document order
- Combined text is returned for reading
- Cross-origin iframes: Gracefully handles iframes that cannot be accessed due to CORS restrictions
- Timeout protection: 5-second timeout for iframe responses
- Fallback behavior: If iframe access fails, continues with main frame content only
- Logging: Warnings logged for inaccessible iframes, success messages for frame aggregation
- Same-origin policy: Respects browser security by only accessing same-origin iframes
- Cross-origin handling: Safely handles cross-origin iframe access failures
- Message validation: Validates message structure and origin before processing
- Timeout protection: Prevents hanging on unresponsive iframes
- Seamless reading: Users get all page content read without manual iframe interaction
- Visual feedback: Console logging provides visibility into frame processing
- Graceful degradation: Falls back to main frame only if iframe access fails
- Performance: Parallel iframe processing for faster text aggregation
The iframe support should be tested on:
- Documentation sites with embedded content (e.g., GitHub Pages with embedded examples)
- Educational platforms with iframe-based lessons
- News sites with embedded widgets or social media content
- Mixed content scenarios with both accessible and restricted iframes
- Cross-origin iframe scenarios to verify graceful handling
- Uses
postMessageAPI for secure cross-frame communication - Maintains backward compatibility with existing single-frame behavior
- Does not require additional permissions beyond existing extension capabilities
- Frame detection is automatic and requires no user configuration
Potential improvements could include:
- User setting to enable/disable iframe reading
- Visual indicators showing which frames contain readable content
- Frame-specific highlighting coordination
- Support for nested iframe hierarchies