0

After executing "chrome.runtime.sendMessage()" in content.js, sometimes "chrome.runtime.onMessage.addListener()" in event.js may not fired.

Immediately after starting the Chrome browser, the process works normally, but after a certain period of time, this phenomenon seems to occur.

If you are experiencing the same phenomenon and someone has solved the problem, please tell me how to solve it.


[content.js]

chrome.runtime.sendMessage(message,function(response){
  :
}

[event.js]

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  :
}

[manifest.json]

   :
"background":{
    "scripts": [
        "event.js"
    ],
    "persistent": false
},
   :

Best Regards,

holyblue
  • 9
  • 1
  • Whenever I had this issue it was due to an unhandled error in my content script or background script that was halting script execution. So check both consoles for error logs. – GoPotato Feb 23 '21 at 02:25
  • This kind of messaging works for every extension so the posted fragments of code are not sufficient to diagnose the problem. We need an [MCVE](/help/mcve) i.e. more relevant info. Currently, I can only guess you reloaded your extension on chrome://extensions page, which [orphanizes the old content scripts](https://stackoverflow.com/a/57471345) so they can't connect anymore. – wOxxOm Feb 23 '21 at 06:07
  • @GoPotato Thank you for your comment! Whenever I had this problem, I have no error in content page console and background page console. I output debug logs for both content.js and event.js. When this problem occurs, the debug log of content.js is output normally, and the debug log of event.js is not output at all. – holyblue Feb 24 '21 at 05:24
  • @GoPotato I tried changing the persistence parameter false to true in the manifest.json. `"background":{ "scripts": [ "event.js" ], "persistent": true }, ` As a result, this problem no longer occurs. I know that the background script is always loading, and the event script is loading every time an event occurs. Therefore, I think that the event script is not loaded even if the event occurs due to some problem. – holyblue Feb 24 '21 at 05:25
  • However, I recognize that it is not recommended to set the persistence parameter to false. – holyblue Feb 24 '21 at 05:25

0 Answers0