Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zgayjjf opened this issue Dec 7, 2021 · 6 comments
Open

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

zgayjjf opened this issue Dec 7, 2021 · 6 comments

Comments

@zgayjjf
Copy link

zgayjjf commented Dec 7, 2021

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?

@coure2011
Copy link

@zgayjjf any update? Did u find any solution?

@aulneau
Copy link

aulneau commented Aug 6, 2022

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?

@farazshuja
Copy link

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

@aulneau
Copy link

aulneau commented Aug 6, 2022

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
Copy link

aulneau commented Aug 6, 2022

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

@farazshuja
Copy link

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants