Browser extension to hide view-reducing screen junk from websites.

bannerbanner/src/ pageActionButton.js
32 lines
834 B

  1. /* Show a page action button on a tab whenever the given predicate is true.
  2. * The predicate is passed a Tab object, and is evaluated whenever the tab has
  3. * changed.
  4. *
  5. * Example usage:
  6. * pageAction({ predicate: tab => tab.url.startsWith('https') })
  7. */
  8. import browser from 'webextension-polyfill'
  9. export default async function pageActionButton({ predicate }) {
  10. async function maybeShowButton(tab) {
  11. if (await predicate(tab)) {
  12. browser.pageAction.show(tab.id)
  13. } else {
  14. browser.pageAction.hide(tab.id)
  15. }
  16. }
  17. // Show/hide the button on current tabs.
  18. const tabs = await browser.tabs.query({})
  19. for (const tab of tabs) {
  20. maybeShowButton(tab)
  21. }
  22. // Show/hide the button whenever a tab changes.
  23. browser.tabs.onUpdated.addListener((id, changeInfo, tab) => {
  24. maybeShowButton(tab)
  25. });
  26. }