0

Please help me figure out why this message is not getting received or responded to:

content.js file:

console.log('running this');
chrome.runtime.sendMessage({
    greeting: 'get-user-data'
}, (response) => {
    // 3. Got an asynchronous response with the data from the service worker
    console.log('received user data', response);
});

background.js file:

console.log('background.js running');
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
    console.log('Message received: ', message);
    // 2. A page requested user data, respond with a copy of `user`
    if (message.greeting === 'get-user-data') {
        sendResponse(user);
        return true;
    }
    return true;
});

manifest.json file:

{
  "manifest_version": 3,
  "version": "1.0",
  "action": {
    "default_popup": "index.html"
  },
  "background": {
    "service_worker": "background.js"
  },
  "content_scripts": [
    {
      "matches": [
        "https://*.myshopify.com/*"
      ],
      "js": [
        "content.js"
      ],
      "run_at": "document_idle"
    }
  ],
  "permissions": [
    "storage",
    "activeTab",
    "tabs",
    "https://*.com/admin/orders/*",
    "scripting",
    "webRequest"
  ]
}

The "running this" console log in the content script prints, and the "background.js running" console log also prints, but none of the other console logs get printed.

I've tried copy/pasting in the example from the extensions docs and it doesn't work, and asked every AI available, but I can't figure out why it's not sending.

A J
  • 3,970
  • 14
  • 38
  • 53
camstar915
  • 133
  • 2
  • 7
  • 1) Your code sends `user` but it isn't defined anywhere. 2) Remove `return true` because your response is sent immediately. 3) Check [the background console](/a/10258029), it should show the error about `user`. – wOxxOm Dec 17 '22 at 18:47
  • Doesn't show errors in the background console, and doesn't show the 'Message received' console log before the user is referenced – camstar915 Dec 17 '22 at 18:54
  • 1
    You either look at the wrong console or the code you've shown in this question is incomplete. You need to define the `user` variable and assign it a value. Also make sure there's no filter in console. Either way, use devtools debugger to set breakpoints and debug the code. – wOxxOm Dec 17 '22 at 19:50
  • You are right there was an error in a different part of the backgroundjs file that killed it, so the listener didn't run. Thanks for the help – camstar915 Dec 17 '22 at 22:46

1 Answers1

1

My test results contradict your claims.

enter image description here

cotent.js

console.log('running this');
chrome.runtime.sendMessage({ greeting: 'get-user-data' }, (response) => {
  // 3. Got an asynchronous response with the data from the service worker
  console.log('received user data', response);
});

background.js

console.log('background.js running');
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  console.log('Message received: ', message);
  // 2. A page requested user data, respond with a copy of `user`
  if (message.greeting === 'get-user-data') {
    const user = "user";
    sendResponse(user);
    return true;
  }
  return true;
});

manifest.json

{
  "name": "hoge",
  "version": "1.0",
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "content_scripts": [
    {
      "matches": [
        "https://nory-soft.web.app/*"
      ],
      "js": [
        "content.js"
      ]
    }
  ]
}
Norio Yamamoto
  • 1,495
  • 2
  • 3
  • 10