1

I am unable to make 2 way communication between content script and popup.

  • I could make a communication with content script and hence alert "Message received is showing up"

  • But feedback alert "response received" is not showing up. But the "response received" alert is showing when I inspect popup and then click on the button.

My question is that why would I need to inspect popup to get feedback alert "response received"? How can I get response without inspecting popup?

Popup HTML

<!doctype html>
<html>
<head>
    <title>activity</title>
<script src="jquery.js"></script>
</head>
<body>
    <button id="clickactivity">click</button>
    <script src="popup.js"></script>
</body>
</html>  

Manifest json

{
  "manifest_version": 2,

  "name": "Such Activity",
  "description": "Wow",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },

  "permissions": [
    "activeTab",
    "tabs",
    "http://*/*",
    "https://*/",
    "<all_urls>"
  ],
  "content_scripts": [
    {
      "matches": [ "https://*/*", "http://*/*" ],
      "js": [ "jquery.js", "content_script.js" ]
    }
  ]
}

Popup HTML

 <!doctype html>
    <html>
    <head>
        <title>activity</title>
    <script src="jquery.js"></script>
    </head>
    <body>
        <button id="clickactivity">click</button>
        <script src="popup.js"></script>
    </body>
    </html>  

Popup JS

function injectTheScript() {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        chrome.tabs.sendMessage(tabs[0].id, { sender: "Hi" }, function (response) {
            if (response.receiver == "Hello") {
                alert("Response recieved");
            }
        });
    });
}
document.getElementById('clickactivity').addEventListener('click', injectTheScript);

Content script JS

$(document).ready(function () {
    chrome.runtime.onMessage.addListener(
      function (request, sender, sendResponse) {
          if (request.sender == "Hi") {   
             alert("Message Received");
              sendResponse({ receiver: "Hello" });
          }
      });
});
nits
  • 19
  • 3
  • Do you know the popup doesn't exist when it's closed (not shown)? – wOxxOm Feb 27 '17 at 15:35
  • 1
    I'm not sure if you are injecting in every page as is shown in the question, but *please* don't load jQuery into **every** page unless you **need** to. jQuery is 85kiB of minimized code. This is a significant burden with which to saddle *every single page*. What of those of us who have 100's of tabs open? While It is possible you really *need* to load jQuery, what's in the question is it's there to save a couple hundred characters in your own code by not using vanilla JavaScript. If that is the case (we have no way to know), doing so is a *very* poor trade-off from your user's point of view. – Makyen Feb 27 '17 at 17:54
  • Note: In your current code `$(document).ready(` is unnecessary. The way you are injecting your scripts already guarantees that they are injected after `DOMContentLoaded` fires. – Makyen Feb 27 '17 at 17:57
  • If your user interaction *begins* with the user clicking a `browserAction` button, then the content script should be injected with [`chrome.tabs.executeScript()`](https://developer.chrome.com/extensions/tabs#method-executeScript) instead of a *manifest.json* `content_script` entry. That way your content script does not burden the browser by being injected into every page just to wait to be used. Using `chrome.tabs.executeScript()`, the script can begin functioning when it is injected with [the data, if any is needed, that has been passed to it](http://stackoverflow.com/a/40815514/3773011). – Makyen Feb 27 '17 at 17:58
  • 1
    Are you trying to do this while viewing `chrome://extensions/`? If you are, it will not work. Have you re-loaded the tab you are viewing after installing/reloading/enabling the extension? If you haven't it will not work. – Makyen Feb 27 '17 at 18:25

0 Answers0