browser.runtime.sendMessage() vs. browser.runtime.getBackgroundPage()

From what I tell, there are two ways that extension pages that open in popup windows (or in a new browser tab) can communicate with the background script: 1) sending messages using browser.runtime.sendMessage(); or, 2) calling browser.runtime.getBackgroundPage() to get a reference to the background page’s Window object, and using that to call the background script functions.

Is one of these two methods preferred or has any advantages over the other?

Definitely prefer “sendMessage” (or “connect” API).

The issues with " getBackgroundPage":

  • doesn’t work from Private windows
  • doesn’t work from Firefox Container windows
  • doesn’t work in upcoming MV3, so if you use it today, it’s a guaranteed technical dept waiting for refactoring in the future