+
Skip to content

Why I can send message from popup to content script by runtime api, but not web-ext #100

@mengxi-ream

Description

@mengxi-ream

for the native api:

// popup.js
const [tab] = await browser.tabs.query({
  active: true,
  currentWindow: true,
});
if (!tab?.id) return;
Send a one-off message
Once you have the tab.id, you can call:

const response = await browser.tabs.sendMessage(tab.id, {
  type: 'POPUP_TO_CONTENT',
  payload: { foo: 123 }
});
console.log('Content script replied:', response);
// content script
browser.runtime.onMessage.addListener((msg, sender) => {
  if (msg.type === 'POPUP_TO_CONTENT') {
    console.log('Popup says:', msg.payload);
    // …do stuff…
    return { ok: true };    // synchronous reply
  }
});

I can successfully send message

but when using web-ext

// popup
const response2 = await sendMessage('testMessage', { message: 'you are a good man' })

// content script
    onMessage('testMessage', (msg) => {
      console.log('testMessage', msg)
      return 'hello'
    })

I got error

@webext-core_messaging.js?v=d757d0d6:1253 Uncaught (in promise) Error: No response
    at @webext-core_messaging.js?v=d757d0d6:1253:67
    at Generator.next (<anonymous>)
    at fulfilled (@webext-core_messaging.js?v=d757d0d6:1211:24)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载