Why is my message being sent from Chrome page action popup not being received by content script (Chrome Extension)?

Viewed 40

I am trying to send a message from my popup to my main content script which will change some values that I have added to a page using the method updateInfo(val). However, I am running into an issue where my message is either not being sent at all or just not being received by content.js. I tried using chrome.runtime.sendMessage(), but that didn't work, so I have went overkill and am sending a message to every tab. My content.js is still not receiving the message. I have confirmed this in the chrome debugger. When I change the select box which lives in the popup, it hits changeYearInfo(), but then the message is never received in content.js.

My content.js:

chrome.runtime.onMessage.addListener(
  function (request, sender, sendResponse) {
    if (request.message === "Year Value Changed") {
      let newVal = request.value;
      updateInfo(newVal);
    }
  });

My popup.js:

function changeYearInfo() {
    let newYearVal = document.getElementById('yearSessions').value;

    chrome.tabs.query({}, tabs => {
        tabs.forEach(tab => {
            chrome.tabs.sendMessage(tab.id, {
                "message": "Year Value Changed",
                "value": newYearVal
            });
      });
    });
}
document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('yearSessions').addEventListener("change", changeYearInfo);
}
);

How would I fix this issue? I've successfully used message passing multiple times from background.js to content.js and vice versa, but this is the first time I've tried sending a message from popup to content.

EDIT: This has been solved. The issue was not in the message passing, but that I was basing my conclusion off of the debugger tool, and I was incorrectly using the popup debugger when trying to check if my message was being received in content.js

0 Answers
Related