webext-bridge icon indicating copy to clipboard operation
webext-bridge copied to clipboard

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

Open zgayjjf opened this issue 4 years ago • 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 avatar Dec 07 '21 12:12 zgayjjf

@zgayjjf any update? Did u find any solution?

coure2011 avatar Apr 11 '22 03:04 coure2011

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?

aulneau avatar Aug 06 '22 19:08 aulneau

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

farazshuja avatar Aug 06 '22 19:08 farazshuja

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 :)

aulneau avatar Aug 06 '22 19:08 aulneau

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 = () => {

aulneau avatar Aug 06 '22 20:08 aulneau

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'

farazshuja avatar Aug 06 '22 20:08 farazshuja