serversideup / webext-bridge

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

Popup has no tabId so can't message between it and background #45

Closed JTInfinite closed 2 years ago

JTInfinite commented 2 years ago

Sending a message from popup to background doesn't seem to work for me.

I am using this package in a Vue project.

//popup.ts

import { createApp } from 'vue'
import App from './Popup.vue'
import '../styles'
import { sendMessage } from 'webext-bridge/popup'

(async () => {
    const resBG = await sendMessage('test-message', { text: "Popup" }, 'background')
    console.log(`[popup] Got ${ resBG }`);

    const resCS = await sendMessage('test-message', { text: "Popup" }, 'content-script')
    console.log(`[popup] Got ${ resCS }`);
})()

const app = createApp(App)
app.mount('#app')

Background receives this message but throws an error:

Uncaught (in promise) TypeError: When sending messages from background page, use @tabId syntax to target specific tab

Background looks like this:

import { onMessage } from 'webext-bridge/background'
onMessage('test-message', async (message) => {
  const { data, sender } = message
  console.log(`[background] Got test message from ${ sender.context }@${ sender.tabId }. Data: ${ JSON.stringify(data) } `);
  return `Hello`

})

that console message shows:

[background] Got test message from popup@NaN. Data: [object Object]

Content script does not receive anything from the popup.

Is it possible to communicate with the popup in this way or have I got it wrong?

Thanks

JTInfinite commented 2 years ago

Closing this as I realise it is basically the same as #37