Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's generally no direct way of accessing a different-origin <code>window</code> object. If you want to <em>securely</em> communicate between content scripts in different frames, you have to send a message to the background page which in turn sends the message back to the tab.</p> <p>Here is an example:</p> <p>Part of <code>manifest.json</code>:</p> <pre><code>"background": {"scripts":["bg.js"]}, "content_scripts": [ {"js": ["main.js"], "matches": ["&lt;all_urls&gt;"]}, {"js": ["sub.js"], "matches": ["&lt;all_urls&gt;"], "all_frames":true} ] </code></pre> <p><code>main.js</code>:</p> <pre><code>var isTop = true; chrome.runtime.onMessage.addListener(function(details) { alert('Message from frame: ' + details.data); }); </code></pre> <p><code>sub.js</code>:</p> <pre><code>if (!window.isTop) { // true or undefined // do something... var data = 'test'; // Send message to top frame, for example: chrome.runtime.sendMessage({sendBack:true, data:data}); } </code></pre> <p>Background script 'bg.js':</p> <pre><code>chrome.runtime.onMessage.addListener(function(message, sender) { if (message.sendBack) { chrome.tabs.sendMessage(sender.tab.id, message.data); } }); </code></pre> <p>An alternative method is to use <code>chrome.tabs.executeScript</code> in <code>bg.js</code> to trigger a function in the main content script.</p> <h2>Relevant documentation</h2> <ul> <li><a href="https://developer.chrome.com/extensions/messaging" rel="noreferrer">Message passing</a> <sup><a href="https://developer.chrome.com/extensions/runtime#method-sendMessage" rel="noreferrer"><code>c.runtime.sendMessage</code></a> / <a href="https://developer.chrome.com/extensions/tabs#method-sendMessage" rel="noreferrer"><code>c.tabs.sendMessage</code></a> / <a href="https://developer.chrome.com/extensions/runtime#event-onMessage" rel="noreferrer"><code>c.runtime.onMessage</code></a></sup></li> <li><a href="https://developer.chrome.com/extensions/runtime#type-MessageSender" rel="noreferrer"><code>MessageSender</code></a> and <a href="https://developer.chrome.com/extensions/tabs#type-Tab" rel="noreferrer"><code>Tab</code></a> types.</li> <li><a href="https://developer.chrome.com/extensions/content_scripts" rel="noreferrer">Content scripts</a></li> <li><a href="https://developer.chrome.com/extensions/tabs#method-executeScript" rel="noreferrer"><code>chrome.tabs.executeScript</code></a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload