serversideup / webext-bridge

💬 Messaging in Web Extensions made easy. Batteries included.
https://serversideup.net/open-source/webext-bridge
MIT License
548 stars 50 forks source link

Messaging from background to popup stuck #37

Open ChxGuillaume opened 2 years ago

ChxGuillaume commented 2 years ago

Whenever my popup is closed, and I use sendMessage it is stuck and waiting for the popup to open

I can't find any fix on my side to check if the tab is contactable or not, any solution ? Is it a bug ?

zikaari commented 2 years ago

Messages get queued for the destination if it is not immediately available.

What behavior were you expecting?

ChxGuillaume commented 2 years ago

My case is maybe somehow not usual, but it is to send updates to the Popup window. However, every time I load the Popup, it already refreshes everything from API calls and the queued messages kinda get in the way and mess everything up.

Would have liked to find a way to send messages to my Popup tab only if it is actually ready to received (Active or Able to execute what it receives)

I tried to make a subscribe system for tabs who wanna received those messages, but can't find a solution to unsubscribe from the Popup whenever it unloads.

zikaari commented 2 years ago

I see. Have you tried fixing this by having the senders wait for the "wake" signal from popup before they send any messages?

Also use webext-bridge@6.0.0-rc3, its better in every way (see the CHANGELOG in the next branch of this repo)

ChxGuillaume commented 2 years ago

Using webext-bridge@6.0.0-rc3 it seems like I am losing auto complete (which is fine)

But I am now having three problems (one I had before and now)

Pic 1: image

Pic 2: image

zikaari commented 2 years ago

popup destination is a tabId agnostic destination, I don't think it is tied to any tab in particular, it just exists, standalone.

Can you please share a minimal repro for the errors you are getting that I can test against?

ChxGuillaume commented 2 years ago

For my background job, I would have:

onMessage('setting-update', async ({ data }) => {
  await database.settings.updateSetting(data)
})

onMessage('settings-fetch', async ({ sender, data }) => {
  console.log('test', sender, data)

  await sendMessage('settings-fetch-response', await database.settings.getSettings(), { ...sender, tabId: data.tabId })
})

and for the popup:

onMessage('settings-fetch-response', ({ data }) => {
  settingsStore.loadSettings(data)
})

browser.tabs.query({ currentWindow: true, active: true })
  .then((data) => {
    tabId.value = data[0]?.id

    if (tabId.value) {
      sendMessage('settings-fetch', { tabId: tabId.value }, 'background')
    }
  })

PS: it works, just sends errors in console

zikaari commented 2 years ago
onMessage('settings-fetch', async ({ sender, data }) => {
  console.log('test', sender, data)

  await sendMessage('settings-fetch-response', await database.settings.getSettings(), { ...sender, tabId: data.tabId })
})

You don't have to use sendMessage inside onMessage if you want to respond with a value. Just return the value from the callback and it'll be available to in the sender by awaiting the sendMessage.

JTInfinite commented 2 years ago

I'm having this same issue. I can get the message to popup from background but I have to use the sendMessage function from background

onMessage('settings-fetch', async ({ sender, data }) => {
  console.log('test', sender, data)

  await sendMessage('settings-fetch-response', await database.settings.getSettings(), { ...sender, tabId: data.tabId })
})

You don't have to use sendMessage inside onMessage if you want to respond with a value. Just return the value from the callback and it'll be available to in the sender by awaiting the sendMessage.

returning the value does not work.

zikaari commented 2 years ago

Can you please install webext-bridge@6.0.0-rc4 and report me any new observations?

vabatta commented 1 year ago

I get the same issue, with both 6.0.1 and 6.0.0-rc4.

Checking the parameter of onMessage when a job is sent from a content script returns sender : {context: 'content-script', tabId: null, frameId: null}.

zikaari commented 1 year ago

I don't have too much experience with popups and the challenges that come with them. I'm also unable to allocate resources to this task, can someone please take on this problem?

vabatta commented 1 year ago

@zikaari Can you point in the code where the wrapping from the underlaying runtime.onMessage to Sender type happens? It would help for taking a look :)

scarletczen commented 7 months ago

I'm having this same issue. I can get the message to popup from background but I have to use the sendMessage function from background

onMessage('settings-fetch', async ({ sender, data }) => {
  console.log('test', sender, data)

  await sendMessage('settings-fetch-response', await database.settings.getSettings(), { ...sender, tabId: data.tabId })
})

You don't have to use sendMessage inside onMessage if you want to respond with a value. Just return the value from the callback and it'll be available to in the sender by awaiting the sendMessage.

returning the value does not work.

I was having a similar issue where I was having problems with returning the values. On carefully reading the documentation, I figured it out. You have to return the PROMISE in case the callback is async, example

onMessage("settings-fetch", async ({ sender, data }) => {
    return database.settings.getSettings(); // returning the promise, not the awaited value
};