Firefox for Android - taking screenshot of page using button in Popup

I’m now porting one of my add-ons for Android and I have a problem with one of the features - taking screenshot of current page using a button in the Popup.

Since Popups in Android are opened as normal pages, when I click the button in the Popup, it will take screenshot of the Popup :slight_smile:

Is there some nice way to wait for the Popup to be closed?

This is my button click:

e => {browser.runtime.sendMessage({type: 'add_current'}); window.close()};

Then background will handle the screenshot capture - and here I would like to wait for the pop-up to be closed and the previous page to be rendered.

NOTE that caputreTab API should solve this issue (because you can specify target tab id) but it seems to be broken in Android: https://bugzilla.mozilla.org/show_bug.cgi?id=1536384

EDIT:
One bug here - seems like Popup in Android cannot be closed with window.close() and one have to use browser.tabs.getCurrent().then(tab => browser.tabs.remove(tab.id)) to close it - this however won’t work in desktop version so you need to detect Android first with something like this browser.runtime.getPlatformInfo().then(({os}) => os === 'android').

EDIT 2:
browser.tabs.captureTab actually doesn’t exists on Android :smiley:

Still the original problem remains.

So it looks like all I needed was a one line of code in my background script:

if (!tab.active) await browser.tabs.update(tab.id, {active: true});

This will activate the tab that needs to be focused and waits until it’s focused. The popup will be closed asynchronously in the meantime.