I'm using a Chrome extension to inject web pages as iframes inside of one of my extension's internal pages.
Stripping the cross-origin headers from the iframes, so, they're showing just fine.
And setting the field "all_frames": true at the extension's manifest, which allows my content-script to be injected in all the pages and the iframes inside of them as well.
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"all_frames": true, //<------
"run_at": "document_idle",
"js": [
"js/content-script.js"
]
}
],
This injects my content-script at ALL the iframes inside the web pages (some websites has iframes at them already, so, we get a nested iframes situation here)..
I need to exchanges messages with only the top-level iframe. I don't care about the nested children iframes inside of the page.
I tried to detect the parent window like this:
if (window !== window.top) {
console.console('iframe');
} else {
console.console('NOT iframe');
}
But, this is always iframe, because, well, I'm injecting the top-level web page itself as an iframe inside of my extension.
How can I detect the top-level iframe only?
So, if this is the case now:
<iframe name="1">
<iframe name="2">
<iframe name="3"></iframe>
</iframe>
</iframe>
How can I detect if this script is running inside of iframe #2 ?