Window.opener not being set

Hi,

I am new to extension development I do not if I am missing something. I am getting the issue where firefox is not setting the window.opener of a popup window which is opened on a button click from my extension.
The popup is from extension.
Here is the code:
window.open(browser.extension.getURL(“template.html”), ‘Title’);

It opens a popup but the window.opener is not set. I need it to send message back to the parent window.
Any help?

Thanks

I think you should use browser.windows.create instead:

And set the type to “popup”:

Thanks, for the reply but it also does not works. It also gives window.opener as null. Also, I am unable to call any browser.windows related function from my main content.js script. I am only able to call browser.windows and browser.runtime.sendmessage related functions from my extensions popup. Any reason why this is happening.
Thanks

Yes, content scripts can use only part of the webextensions API: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#WebExtension_API

But you can use browser.runtime.sendMessage to send message to your background script (which can open the window and return tabId.

Know that message send with browser.runtime.sendMessage will be received by all parts of your addon, including your new window.

However if you want to target only the new window, you should use browser.tabs.sendMessage and use the tabId returned by browser.windows.create. Again you can do this only from background script. So if you want to use it from your content script, you need to forward the message through background script, for example:

browser.runtime.onMessage.addListener((data, sender) => {
  switch (data.type) {
    // return promise that will resolve to whatever the target tab replies (again by returning a promise)
    case 'forwardToTab': return browser.tabs.sendMessage(data.targetTabId, data.customData);
  }
});

Thanks this worked but I had to change my structure. Still not getting why window.open is giving deadobject and why window.opener of the newly opened window is not being set.