In chrome extension, how to send a cross-origin message from a parent content script to a content script in specific child iframe
<p>I am developing a Chrome extension with a manifest that, for now, enables access to all hosts. The background script injects content scripts into all frames. After the DOM is loaded, the content script in the top page/frame begins to walk the DOM tree. When the walker encounters an iframe, it needs to message the specific content script associated with that iframe's window (possibly cross-origin) to begin it's work and includes some serialized data with this message. The parent window suspends execution and waits for the child to complete it's walk and send a message back that it is done along with serialized data. The parent then continues its work. I have tried two approaches to this problem:</p>

<ol>
<li><code>frameElement.contentWindow.postMessage</code>: this works most of the time, but not always. Sometimes the message is never received by the content script message event listener associated with the iframe window. I have not been able to confirm the cause but I think it is listeners attached before my listener calling <code>event.stopImmediatePropagation()</code>. For example, on the yahoo home page (<a href="https://www.yahoo.com" rel="noreferrer">https://www.yahoo.com</a>), when posting a message to my content script associated with iframe source <a href="https://s.yimg.com/rq/darla/2-9-9/html/r-sf.html" rel="noreferrer">https://s.yimg.com/rq/darla/2-9-9/html/r-sf.html</a>, the message is never received. This is an ad-related iframe. Maybe the blocking of messages is intentional. There is no error when the message is posted and I use a targetOrigin of "*".</li>
<li><code>chrome.runtime.sendMessage</code>: I can send a message to the background page but cannot figure out how to tell the background page to which frame to relay the message. The parent window content script does not know the chrome extension frameId associated with the child frame element it encountered in the DOM walk. So it cannot tell the background page how to direct the message.</li>
</ol>

<p>For point 2, I have tried two techniques that I found here on stackoverflow:</p>

<ol>
<li>Using concept described in this <a href="https://stackoverflow.com/questions/26010355/is-there-a-way-to-uniquely-identify-an-iframe-that-the-content-script-runs-in-fo/">question</a>: In parent window, determine iframe's position in the <code>window.frames</code> array and post a message to the background page with this index. The background page posts a message to all frames with the desired index in the message data. Only the iframe that finds it's window object position in the window.parent.frames array matches the index received from the message proceeds with it's walk. This works OK but is vulnerable to changes in the <code>window.frames</code> array during the asynchronous messaging process (if an iframe is deleted after message is sent, the index value may no longer match the desired frame).</li>
<li>Instead of the index value from point 1, use <code>frameElement.name</code> in the parent window. With same messaging technique, send name to child iframe for comparison to its <code>window.name</code> value. I believe <code>window.name</code> gets it's value from the <code>frameElement.name</code> at the time of the iframe element creation. However, since I don't control the frame element creation, the name attribute is often an empty string and can't be relied on to uniquely match iframe elements to their windows.</li>
</ol>

<p>Is there a way for me to reliably send a message to a content script associated with an iframe element found in walking a DOM tree?</p>