-
Notifications
You must be signed in to change notification settings - Fork 38
Open
Description
Alright, I found one more problem that lazy onMessage.
If do awaited sendMessage on the CS side and then do onMessage, the message not be received. The current implementation doesn't have handshake and polling, so all onMessage must be done first on both sides. Otherwise, the message will be unreachable.
injected script
export default defineUnlistedScript(async () => {
console.log('[google-injected.ts] Injected script loaded');
googleMessaging.onMessage('ping', event => {
console.log('[google-injected.ts] Received', event);
return 'pong';
});
googleMessaging.onMessage('ping2', event => {
console.log('[google-injected.ts] Received2', event);
return 'pong2';
});
// The receiver is not there yet.
googleMessaging.sendMessage('fromInjected', undefined).then(res => {
console.log('[google-injected.ts] Response:', res);
});
// The receiver is not there yet.
const res2 = await googleMessaging.sendMessage('fromInjected2', undefined);
console.log('[google-injected.ts] Response2:', res2);
});content-scirpt
export default defineContentScript({
matches: ['*://*.google.com/*'],
main(ctx) {
console.log('[google.content.ts] Content script loaded');
const script = document.createElement('script');
script.src = browser.runtime.getURL('/google-injected.js');
script.onload = async () => {
const res = await googleMessaging.sendMessage('ping', undefined);
console.log('[google.content.ts] Response:', res);
const res2 = await googleMessaging.sendMessage('ping2', undefined);
console.log('[google.content.ts] Response2:', res2);
// ! Message has already been sent.
googleMessaging.onMessage('fromInjected', event => {
console.log('[google.content.ts] Received:', event);
return 'hello injected';
});
// ! Message has already been sent.
googleMessaging.onMessage('fromInjected2', event => {
console.log('[google.content.ts] Received:', event);
return 'hello injected2';
});
};
document.head.appendChild(script);
},
});Originally posted by @1natsu172 in #57 (comment)
expectation
The expectation might be that an error like “no receiver” will be returned.
Metadata
Metadata
Assignees
Labels
No labels