Skip to content

Instantly share code, notes, and snippets.

@bcruddy
Created December 28, 2020 16:38
Show Gist options
  • Save bcruddy/fc99873729883223035c69ed68c7432c to your computer and use it in GitHub Desktop.
Save bcruddy/fc99873729883223035c69ed68c7432c to your computer and use it in GitHub Desktop.
function forEachFrame (fn, targetWindow = window, filter = () => null) {
const frames = targetWindow.document.querySelectorAll('iframe, frame');
for (const frame of frames) {
if (filter(frame)) {
continue;
}
try {
fn(frame.contentWindow, frame);
} catch (err) {
console.warn('failed to access child frame', err.toString());
}
try {
forEachFrame(fn, frame.contentWindow);
} catch (err) {
console.warn('failed to access nested child frame', err.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment