|
- /* Show a page action button on a tab whenever the given predicate is true.
- * The predicate is passed a Tab object, and is evaluated whenever the tab has
- * changed.
- *
- * Example usage:
- * pageAction({ predicate: tab => tab.url.startsWith('https') })
- */
-
- import browser from 'webextension-polyfill'
-
-
- export default async function pageActionButton({ predicate }) {
- async function maybeShowButton(tab) {
- if (await predicate(tab)) {
- browser.pageAction.show(tab.id)
- } else {
- browser.pageAction.hide(tab.id)
- }
- }
-
- // Show/hide the button on current tabs.
- const tabs = await browser.tabs.query({})
- for (const tab of tabs) {
- maybeShowButton(tab)
- }
-
- // Show/hide the button whenever a tab changes.
- browser.tabs.onUpdated.addListener((id, changeInfo, tab) => {
- maybeShowButton(tab)
- });
- }
|