Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This error seems to be happening because the content script has not yet been injected into the page when your background script sends the message. Hence, the "receiving end does not exist."</p> <p>I'm assuming (because I don't have more than 50 rep to be able to comment on your question and clarify this first, so correct me if I am wrong) that in your manifest.json file, you specify it in the following way:</p> <pre><code>"content_scripts": [{ "matches": ["*://xyz.com/*"], "js": ["contentscript.js"] }] </code></pre> <p>If this is indeed how you are injecting the content script, then you need to know that the content script only gets injected after the DOM has finished rendering. (Search for 'run_at' on the following link: <a href="http://developer.chrome.com/extensions/content_scripts.html" rel="nofollow">http://developer.chrome.com/extensions/content_scripts.html</a>) This means that when you send that message from the background script, the content script is still "loading."</p> <p>The good news is that you can specify when the content script should be loaded by adding a third key-value pair to the content_scripts parameter in the manifest.json file, like so:</p> <pre><code>"content_scripts": [{ "matches": ["*://xyz.com/*"], "js": ["contentscript.js"], "run_at": "document_start" }] </code></pre> <p>This tells the extension that you want to inject the contentscript.js before the DOM is constructed or any other script is run (i.e. as early as possible).</p> <p>If the above technique still gives you the same error, that's an indication that even document_start is not early enough. In that case, let's consider another approach entirely. What you are attempting to do presently is to have the background script connect to the content script. Why not have the content script connect to the background script instead when it has successfully been injected into the page? The background page is <strong>always</strong> running so it is guaranteed to be able to receive the message from the content script without complaining about "the receiving end does not exist." Here is how you would do it:</p> <p>In background.js:</p> <pre><code>chrome.runtime.onConnect.addListener(function(port) { console.log("background: received connection request from content script on port " + port); port.onMessage.addListener(function(msg) { console.log("background: received message '" + msg.action + "'"); switch (msg.action) { case 'init': console.log("background script received init request from content script"); port.postMessage({action: msg.action}); break; } }); }); </code></pre> <p>In contentscript.js:</p> <pre><code>var port_to_bg = chrome.runtime.connect({name: "content_to_bg"}); port_to_bg.postMessage({action: 'init'}); port_to_bg.onMessage.addListener(function(msg) { switch (msg.action) { case 'init': console.log("connection established with background page!"); break; } } </code></pre> <p>Feel free to ask more questions for clarification! I'd be curious to learn if the first approach worked. If not, the second approach is a sure win.</p>
    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