My goal is to simply change the browser action icon to reflect whether the extension has or doesn’t have the host permission to access the current tab.
Currently there doesn’t seem to be a good way to detect whether an extension has permissions to access the currently active tab in the background.
One cannot use browser.permissions.contains()
without having the “tabs” permission (“tabs” permission is needed to get the URL of the active tab).
My current workaround works by trying to inject a content script and see if it succeeds or fails:
async function hasHostPermissionForTab(tabId) {
try {
return (await browser.scripting.executeScript({
target: { tabId: tabId },
injectImmediately: true,
func: () => true,
}))[0].result;
}
catch(_) {
return false;
}
}