"TypeError: browser.tabs is undefined"?

This is the first time I’m writing an extension so I apologise if I’ve missed something obvious.

When I’m calling browser.tabs.getCurrent() from inside a content script with both the “activeTab” and the “tabs” permission I get the aforementioned error. Any help would be much appreciated.

Content scripts only get a subset of the extension APIs: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#webextension_apis. You’ll likely want to send the data the content script needs to function via runtime.sendMessage, or even better a reply to a request the content script sends (to the background page)

Thanks for the help!

I’m trying to send a message from the popup script to the content script. The page for runtime.sendMessage mentions that it’s not possible to send messages to content scripts and suggests tabs.sendMessage, for which I’d need the tabID which is why I was trying to call getCurrent from inside the content script.

Would the proper way to do this be calling tabs.query from inside the popup script to get the tabID?

Yes, from the popup you can get active tab with a

const [activeTab] = await browser.tabs.query({active: true, lastFocusedWindow: true});

warning: advanced features detected :smiley:

If the content script is always injected, yes. If the popup injected the content script, then the content script should use sendMessage to ask the popup for the details. Note that as soon as the popup closes, it can no longer process any communication with the content script.

Thanks. That hepled me, too.