A lightweight TypeScript library for recursively finding nested iframes in the DOM tree.
- 🔍 Recursive Search - Find iframes at any nesting depth
- 🎯 Multiple Search Methods - By id, name, or custom criteria
- 🛡️ Safe - Handles cross-origin iframes gracefully
- 📦 Lightweight - Zero dependencies, ~1KB gzipped
- 💪 TypeScript - Full type safety and IntelliSense support
- ⚡ Fast - Optimized search with depth limiting
npm install iframe-finderimport { findIframeById } from 'iframe-finder';
// Find iframe with id="myFrame" anywhere in the DOM tree
const iframe = findIframeById('myFrame');
if (iframe) {
console.log('Found iframe:', iframe);
// Access iframe content
const content = iframe.contentDocument;
}import { findIframeByName } from 'iframe-finder';
const iframe = findIframeByName('contentFrame');import { findElement } from 'iframe-finder';
// Find element by selector anywhere in iframe hierarchy
const healthTab = findElement('[title="Character Info"]');
// No need to know which iframe contains the element!
const button = findElement('#submitButton');
// Works with any CSS selector
const element = findElement('.special-class');import { findAllElements } from 'iframe-finder';
// Find all matching elements across all iframes
const allButtons = findAllElements('button.submit');
// Find all elements with specific attribute
const allLinks = findAllElements('a[target="_blank"]');import { findIframe } from 'iframe-finder';
// Find iframe by src
const iframe = findIframe((frame) =>
frame.src.includes('example.com')
);
// Find iframe by class
const iframe = findIframe((frame) =>
frame.classList.contains('special-frame')
);
// Find iframe by data attribute
const iframe = findIframe((frame) =>
frame.dataset.type === 'content'
);import { findIframeById } from 'iframe-finder';
// Limit search depth
const iframe = findIframeById('myFrame', {
maxDepth: 3 // Only search 3 levels deep
});
// Start from specific document
const iframe = findIframeById('myFrame', {
rootDocument: someIframe.contentDocument
});import { findAllIframes } from 'iframe-finder';
// Find all iframes with specific class
const iframes = findAllIframes((frame) =>
frame.classList.contains('content')
);
console.log(`Found ${iframes.length} iframes`);Recursively searches for an iframe by its id attribute.
Parameters:
id- The id attribute to search foroptions- Optional search options
Returns: The found iframe element or null
Recursively searches for an iframe by its name attribute.
Parameters:
name- The name attribute to search foroptions- Optional search options
Returns: The found iframe element or null
Recursively searches for an iframe using a custom predicate function.
Parameters:
predicate- Function that returnstruewhen iframe matches criteriaoptions- Optional search options
Returns: The found iframe element or null
Finds all iframes matching the predicate (single level, non-recursive).
Parameters:
predicate- Function to test each iframerootDocument- Starting document (defaults towindow.document)
Returns: Array of matching iframes
interface FindIframeOptions {
rootDocument?: Document; // Starting document (default: window.document)
maxDepth?: number; // Maximum search depth (default: Infinity)
}type IframePredicate = (iframe: HTMLIFrameElement) => boolean;import { findIframeById } from 'iframe-finder';
// Game UI often has deeply nested iframes
const showInfoFrame = findIframeById('showInfo');
if (showInfoFrame?.contentDocument) {
const healthElement = showInfoFrame.contentDocument
.querySelector('[title="Character Info"]');
}import { findIframe } from 'iframe-finder';
// Find advertisement iframe
const adFrame = findIframe((frame) =>
frame.src.includes('doubleclick.net') ||
frame.classList.contains('ad-frame')
);import { findIframeByName } from 'iframe-finder';
// Find chat or messaging iframe
const chatFrame = findIframeByName('chat-widget');
if (chatFrame) {
// Setup postMessage communication
chatFrame.contentWindow?.postMessage({ type: 'init' }, '*');
}Works in all modern browsers that support:
querySelector/querySelectorAllHTMLIFrameElement.contentDocumentArray.from
This includes:
- Chrome/Edge 45+
- Firefox 40+
- Safari 10+
- Opera 32+
- Cross-origin iframes are automatically skipped due to browser security restrictions
- The library catches and silently handles cross-origin access errors
- Always validate and sanitize any data retrieved from iframe content
MIT © Artem Deikun
Contributions are welcome! Please feel free to submit a Pull Request.