0

My content.js sends chrome.runtime.sendMessage and popup.js will get the chrome.runtime.onMessage.addListener.

Currently, I can only receive the message if the Popup Icon is being clicked.

My questions are:

  1. How can I receive the chrome.runtime.onMessage.addListener even if the Popup is not open?

  2. If I open the Popup Extension and close it, how can I still receive the chrome.runtime.onMessage.addListener from content.js?

user16691768
  • 93
  • 1
  • 8
  • 1
    The popup runs only when shown so no. Reverse the direction and use chrome.tabs.sendMessage in the popup to ask the content script. – wOxxOm Aug 20 '21 at 20:05
  • Ohh. Its you again. Thanks for always answering. By the way, this is my code. https://stackoverflow.com/questions/68847223/how-can-i-reload-popup-without-closing-to-retrieve-new-data-from-content?noredirect=1#comment121678418_68847223 . You mean add `chrome.tabs.sendMessage` or replace `chrome.runtime.onMessage.addListener` in popup into `chrome.tabs.sendMessage`? – user16691768 Aug 20 '21 at 20:27
  • At the beginning of the popup script you will use chrome.tabs.query to get the active tab's id and then use it in chrome.tabs.sendMessage. The content script will have onMessage listener, which will scrape the data and send the results back by calling sendResponse, [example](https://developer.chrome.com/extensions/messaging). – wOxxOm Aug 20 '21 at 20:32

0 Answers0