3

Note: I posted this question and someone marked it as a duplicate of this question. In that question, the solution is to inspect the popup rather than looking in the normal devtools. My issue is not that I don't know where to look, it's that the popup/background script console is not behaving as I would expect it to. In my testing, chrome.runtime.sendMessage is not passing a message to the background script.

First time building a manifest v3 extension (or any extension for that matter).

I'm trying to follow along with Google's Message Passing documentation. It works fine when sending a message from my content script to my background service worker on extension load. Now I'm trying to figure out how to send messages to the background service worker from my popup.html. I'm using React in this extension.

content-script.js

console.log("Content script was loaded");
chrome.runtime.sendMessage({ greeting: "hello" }, function (response) {
  console.log(response.farewell);
});

background.js

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  console.log(
    sender.tab
      ? "from a content script:" + sender.tab.url
      : "from the extension"
  );
  if (request.greeting == "hello") sendResponse({ farewell: "goodbye" });
});

app.js

/*global chrome*/
import React from "react";
import "./App.css";

const onClick = () => {
  console.log("The button was clicked");
  chrome.runtime.sendMessage({ greeting: "hello" }, function (response) {
    console.log(response.farewell);
  });
};

function App() {
  return (
    <div>
      <p>Hello</p>
      <button
        onClick={() => {
          onClick();
        }}
      >
        Click to send message
      </button>
    </div>
  );
}

export default App;

manifest.json

{
  "name": "Chrome Extension Starter Kit",
  "version": "1.0",
  "manifest_version": 3,
  "action": {
    "default_popup": "index.html"
  },

  "background": {
    "service_worker": "background.js"
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content-script.js"]
    }
  ]
}

When I load and inspect the extension background console by right clicking the extension and clicking "inspect popup" and then click the button from App.js, I see the following console logs. enter image description here

I would expect to see 'goodbye' logged along with 'This button was clicked' anytime I click the button in App.js.

Is this behavior due to the service worker going inactive? I thought the listeners on the service worker would trigger it to go back to being active?

I also thought it might be because I didn't add App.js as a content-script, so I tried that as well and no luck.

Anyone know how I can get a button from App.js to communicate with the background script?

user12457151
  • 853
  • 2
  • 12
  • 25
  • Judging by your answer, in order for the question to have a proper [MCVE](/help/mcve) you need to mention that external library, which will make the question much easier to find for other people who use that library. – wOxxOm Apr 19 '21 at 17:29
  • I'm definitely still learning how to write a good MCVE, and totally see that now. Will keep in mind for future questions. Thanks for the feedback @wOxxOm! – user12457151 Apr 19 '21 at 17:31

1 Answers1

3

I figured it out!

I was using a chrome extension boiler plate library, and in the index.js file I found a line serviceWorker.unregister();. I commented this out and everything worked as expected.

user12457151
  • 853
  • 2
  • 12
  • 25
  • 2
    Ping the author of that extension boilerplate library and inform them that ManifestV3 extensions should not register or unregister the service worker as it happens automatically. – wOxxOm Apr 19 '21 at 17:25
  • Yep, just let them know on Github. – user12457151 Apr 19 '21 at 17:27
  • Hi I was just wondering which boiler plate you were using. I'm trying to get a click in a popup to invoke a background onMessage but it never fires as well in v3 using https://github.com/lxieyang/chrome-extension-boilerplate-react – TheLearningDev Apr 30 '21 at 02:06
  • @TheLearningDev that's actually the one I am using! I was able to get it work. – user12457151 Apr 30 '21 at 06:46
  • Have you tried using a chrome.tabs.create (I can't get the callback to ever fire on that one if using it in the background) in v3 – TheLearningDev May 05 '21 at 06:30