-
Notifications
You must be signed in to change notification settings - Fork 36
Open
Description
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
Labels
No labels