serversideup/webext-bridge

How can I send&receive messages from a new popped up window?

Opened this issue · 6 comments

I'm using webext-bridge in a new project.

However, I can't receive a response when I use sendMessage in a window opened by the code below.

browser.windows.create({
  focused: true,
  url: `notification.html`,
  type: 'popup',
})

It is the same even if I called allowWindowMessaging in content-script and setNamespace in the created window.

Did I miss something?

@zgayjjf any update? Did u find any solution?

This is still an issue. currently getting around it by using browser.tabs.sendMessage, but seems like a hack. @zikaari @antfu any success working with this?

It should allow to return some custom type from method: getBackgroundPageType which behaves same as options or popup.

https://github.com/zikaari/webext-bridge/blob/main/src/utils.ts#L21-L38

It should allow to return some custom type from method: getBackgroundPageType which behaves same as options or popup.

https://github.com/zikaari/webext-bridge/blob/main/src/utils.ts#L21-L38

Would you elaborate a bit -- not following exactly :)

Edit: Reading that utility, seems like it would return background if you're sending a message from any new window, which is not technically correct.

@antfu @zikaari it would seem that the context is set to background for any additional pages, eg if you have a new popup window, it acts as if it's background and not popup or options. I've been playing around with editing it to add one more check, but I'm not sure what to do :)

Ah, here we go. I need to also check for /notification/index.html, and when I do this diff, it works as expected:

diff --git a/node_modules/webext-bridge/dist/index.mjs b/node_modules/webext-bridge/dist/index.mjs
index 55f1afc..c9231a3 100644
--- a/node_modules/webext-bridge/dist/index.mjs
+++ b/node_modules/webext-bridge/dist/index.mjs
@@ -27,7 +27,7 @@ var getBackgroundPageType = () => {
   const popupPage = ((_a = manifest.browser_action) == null ? void 0 : _a.default_popup) || ((_b = manifest.action) == null ? void 0 : _b.default_popup);
   if (popupPage) {
     const url = new URL(browser.runtime.getURL(popupPage));
-    if (url.pathname === window.location.pathname)
+    if (url.pathname === window.location.pathname || window.location.pathname === '/notification/index.html')
       return "popup";
   }
   if ((_c = manifest.options_ui) == null ? void 0 : _c.page) {
@@ -40,6 +40,7 @@ var getBackgroundPageType = () => {

Edit: Reading that utility, seems like it would return background if you're sending a message from any new window, which is not technically correct.

Yeah exactly. So I m suggesting to make it generic e.g. allow some 'xyz' folder i.e. if the pages are in 'xyz' then the context should be treated as 'options'