2

I'm trying to save some data when the popup closes, but I can't find a place to add a listener. There was an earlier post suggesting this in the background page, but it doesn't work:

chrome.runtime.onConnect.addListener(function (externalPort) {
  externalPort.onDisconnect.addListener(function () {
    console.log("onDisconnect")
  })
  console.log("onConnect")
})

Not even the onConnect listener is working. Any ideas on how to get it to work>

Rico Kahnert
  • 75
  • 1
  • 6
  • 2
    Assuming you also used chrome.runtime.connect() as shown in [this question](https://stackoverflow.com/questions/39730493/) the problem seems to be that you're looking at the wrong console. See [Where to read console messages from background.js in a Chrome extension?](https://stackoverflow.com/q/10257301) – wOxxOm Jun 20 '20 at 16:14
  • Not looking at the wrong console. When I add a log before adding the listener I can see that. I also set a breakpoint on the first line and it stops there when I reload the extension. – Rico Kahnert Jun 20 '20 at 16:38
  • Something is missing. Can't say what without [MCVE](/help/mcve). That question + answer I linked contains a fully working solution. – wOxxOm Jun 20 '20 at 16:40
  • @wOxxOm Sorry, was so focused on the background page. Yes ... the connect was the missing piece. TY! – Rico Kahnert Jun 20 '20 at 16:45

1 Answers1

1

I actually took a different approach. Instead of listening for events, I move the state to the background page. So when the popup opens up it reads the state form the background page and the user can continue where he/she left off.

Rico Kahnert
  • 75
  • 1
  • 6