getBackgroundPage in private mode

Hello,
my addon uses a background script which collects information (using webRequest API, so I think I need to use a background script) and it has a browser action which opens a popup. The popup needs to get some info from the background script. For this, the popup script calls runtime.getBackgroundPage, and this works properly in normal tabs/windows. However, for tabs/windows in private mode, getBackgroundPage always returns (or more precisely, gets fulfilled with) null. Is this intended? If so, what would be the right approach for communicating between popup and background script?

1 Like

Hi,

I am having the same issue. The popup scripts returns null for getBackgroundPage. Please resolve the issue.

To be honest, I don’t really see how this improves the behavior of an extension, but I also don’t think that it will change soon.

what would be the right approach for communicating between popup and background script?

You will have to use this:

Thanks for the reply. So it is intended? Is this documented anywhere? I think it should be. Perhaps their should be separate instances of the background page for private mode?

Now I’m trying to use the runtime.connect approach. Right away I noticed that it’s not possible to send arbitrary JS objects, e.g. the object I’m sending contains a Map instance, but this gets silently dropped. The documentation does say that one can only send “JSON objects”, which I guess means objects which can be completely expressed in JSON. :persevere:

Chrome can do that, and if you decide to go with a single instance (and the user explicitly enables the extension for incognito) it won’t load extension tabs in the incognito windows. I haven’t tested panels yet.

I guess means objects which can be completely expressed in JSON.

Exactly that.

A little update on this:

The mozilla sources contain a test case browser/components/extensions/test/browser/browser_ext_incognito_views.js in which it says: "Should not be able to access background page in incognito context".
So this is intended behavior.

On the other hand it does not happen if Firefox starts in the Private-only mode, so there should always be at least one window that can hold tabs that do have access to the background page.

Since Chrome (in spanning mode) won’t open extension tabs in incognito windows at all (network error), I think it is acceptable to tell the user to open extension pages in a non-private window in Firefox too (or rather, do that automatically).
Especially because there are other problems too, for example blob:-URLs created in the non-private context won’t be available in private windows.

Which still leaves the problem which panels in Firefox Private Windows.
What I decided to do is to open a (non-private) poup window at the same spot where the panel would be. Then I manually implement the close-on-blur behavior. The visual difference is quite small and the performance can be acceptable too.

My implementation can be found here:

Hi, new here and trying to understand how this works.

So I have a popup that uses the background page’s variables and functions directly, accessed via runtime.getBackgroundPage.

But in order for the popup to work in private mode, I’m supposed to have the popup use runtime.connect to send a JSON message to the background, then background does its jobs, and then sends results in a JSON message back to the popup. Like that?

And does this mean for an extension to support private mode, I should forget about runtime.getBackgroundPage altogether? Or should I accommodate with both pathways, one for private one for normal?

For what it’s worth: In popup.js, I use browser.runtime.sendMessage() to request data I want or to send changes to global vars. In background.js I use browser.runtime.onMessage.addListener() to respond to the requests. I don’t know whether it is better to use browser.runtime.connect().

Example:

Disclaimer: As you can tell, I’m not a trained programmer.

Hi

I had this exact problem before doing a refactor of my addon.

In my non background page process I do this:

let port = browser.runtime.connect({
    name: 'someUniqueName'
})
...
something.addEventListener('change', () => {
    ...
    port.postMessage({
        thisCanBeAnyJSONObject: true
    })
}

Then in my background process I have this

browser.runtime.onConnect.addListener((port) => {
    if (port.name === 'someUniqueName') {
        port.onMessage.addListener((msg) => {
            // here the `msg` variable holds the JSON we sent from the non background script
            // // I just needed to check for a flag, in your case you probably need to deseralise your data
            if (msg.thisCanBeAnyJSONObject) {
                doSomethingThing()
            }
        })
    }
})

This setup a control flow of user clicks element -> message background page -> background page does something

The advantage of using a Port is you can establish two way communication if you later need to add extra steps such as a control flow of
Non background process -> Background process -> Non background process again
by registering a listener to the port in your non background process as well.

Note the functions for Port based communication with content scripts are different :confused: