IndexedDB in Containers - contextualIdentities

Hello,

I’m now accessing IndexedDB from my extension page and I just found out, that if I open my extension page in different container, it won’t have access to the same database!

Can I detect that the tab is opened in a container without contextualIdentities permission? (because if I know it’s container tab, I can ask my background script for data from database)

Also Optional permission for contextualIdentities is not yet implemented.

I think you have access to the background page exactly iff your page is executed in the same storage context, i.e.:

async function iAmInNormalContext() {
    return (await browser.runtime.getBackgroundPage()) != null;
}

Note1: In Firefox, the synchronous version on extension works as well.
Note 2: In Chrome, this would load the “event” background page if suspended.

1 Like

Nice workaround! Thank you!
This will also identify Private Browsing tab (where IndexedDB is also inaccessible).

I just hope this will be working also in the future… (at least for a year)

There is actually a race condition bug… if the code is called right after browser starts (if add-on page is set as homepage), then await browser.runtime.getBackgroundPage() will return null, even though it’s not a container.

And if you are “smart” like me and store the result in constant:

const IS_CONTAINER_OR_PRIVATE_TAB_PROMISE = browser.runtime.getBackgroundPage().then(bc => bc === null);

then it will keep report wrong value until you refresh the tab :frowning:.
It took me two hours to track this bug down! Crazy!