How to use the webextension-polyfill.contextMenus function in webextension-polyfill

To help you get started, we’ve selected a few webextension-polyfill examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ipfs-shipyard / ipfs-companion / add-on / src / lib / context-menus.js View on Github external
async update (changedTabId) {
      try {
        if (changedTabId) {
          // recalculate tab-dependant menu items
          const currentTab = await browser.tabs.query({ active: true, currentWindow: true }).then(tabs => tabs[0])
          if (currentTab && currentTab.id === changedTabId) {
            ipfsContext = ipfsPathValidator.isIpfsPageActionsContext(currentTab.url)
          }
        }
        const ifApi = getState().peerCount > 0
        for (const item of apiMenuItems) {
          await browser.contextMenus.update(item, { enabled: ifApi })
        }
        for (const item of ipfsContextItems) {
          await browser.contextMenus.update(item, { enabled: ipfsContext })
        }
        for (const item of apiAndIpfsContextItems) {
          await browser.contextMenus.update(item, { enabled: (ifApi && ipfsContext) })
        }
      } catch (err) {
        log.error('Error updating context menus', err)
      }
    }
github rejerry / bookmark / src / background.js View on Github external
const setupContextMenus = async () => {
  await browser.contextMenus.removeAll();
  const contexts = [browser.contextMenus.ContextType.BROWSER_ACTION];
  contexts.push(browser.contextMenus.ContextType.PAGE);
  const menus = {
    show_list: tabs.openTabLists,
    store_selected_tabs: tabs.storeSelectedTabs,
    store_left_tabs: tabs.storeLeftTabs,
    store_right_tabs: tabs.storeRightTabs,
    store_twoside_tabs: tabs.storeTwoSideTabs,
    store_all_tabs: tabs.storeAllTabs,
    store_all_in_all_windows: tabs.storeAllTabInAllWindows,
  };
  const createMenus = async (obj, parent) => {
    for (const key of Object.keys(obj)) {
      const prop = {
        id: key,
        /* "menu_" + menus.props == i18 msg item。如果不存在会显示错误*/
        title: __('menu_' + key),
github cnwangjie / better-onetab / src / background / init.js View on Github external
const init = async () => {
  logger.init()
  await listManager.init()
  const opts = await initOptions()
  await updateBrowserAction(opts.browserAction)
  await setupContextMenus(opts)
  await Promise.all([
    browser.commands.onCommand.addListener(commandHandler),
    browser.runtime.onMessageExternal.addListener(commandHandler),
    browser.runtime.onMessage.addListener(messageHandler),
    browser.runtime.onUpdateAvailable.addListener(detail => { window.update = detail.version }),
    browser.runtime.onInstalled.addListener(installedEventHandler),
    browser.browserAction.onClicked.addListener(() => window.browswerActionClickedHandler()),
    browser.contextMenus.onClicked.addListener(info => window.contextMenusClickedHandler(info)),
    browser.tabs.onActivated.addListener(_.debounce(tabsChangedHandler, 200)),
    browser.storage.onChanged.addListener(storageChangedHandler),
  ])
  await migrate()
  await fixDirtyData()
  await boss.init()
}
github mar-kolya / secure-password-generator / src / background.js View on Github external
}
    password = shuffle(password);

    return password;
}

const MENU_CONTEXTS = "PASSWORD" in browser.contextMenus.ContextType
      ? [browser.contextMenus.ContextType.PASSWORD, browser.contextMenus.ContextType.EDITABLE] : [browser.contextMenus.ContextType.EDITABLE];

browser.contextMenus.create({
    id: constants.GENERATE_PASSWORD_MENU,
    title: browser.i18n.getMessage("menuLabelGenerate"),
    contexts: MENU_CONTEXTS
});

browser.contextMenus.create({
    id: constants.INSERT_PREVIOUS_PASSWORD_MENU,
    title: browser.i18n.getMessage("menuLabelInsertPrevious"),
    contexts: MENU_CONTEXTS
});

try {
    browser.contextMenus.create({
	id: constants.OPEN_POPUP_MENU,
	title: browser.i18n.getMessage("menuLabelOpenPopup"),
	contexts: MENU_CONTEXTS,
	command: "_execute_browser_action"
    });
} catch(error) {
    console.info("Cannot install open_popup menu item, probably running on chrome: ", error);
}
github cnwangjie / better-onetab / src / background.js View on Github external
browser.contextMenus.update('STORE.STORE_LEFT_TABS', {
    enabled: groupedTabs.left.length !== 0,
    title: __('menu_STORE_LEFT_TABS') + ` (${groupedTabs.left.length})`,
  })
  browser.contextMenus.update('STORE.STORE_RIGHT_TABS', {
    enabled: groupedTabs.right.length !== 0,
    title: __('menu_STORE_RIGHT_TABS') + ` (${groupedTabs.right.length})`,
  })
  browser.contextMenus.update('STORE.STORE_TWOSIDE_TABS', {
    enabled: groupedTabs.twoSide.length !== 0,
    title: __('menu_STORE_TWOSIDE_TABS') + ` (${groupedTabs.twoSide.length})`,
  })
  browser.contextMenus.update('STORE.STORE_ALL_TABS_IN_ALL_WINDOWS', {
    enabled: windows.length > 1,
  })
  browser.contextMenus.update('STORE.STORE_ALL_TABS_IN_CURRENT_WINDOW', {
    title: __('menu_STORE_ALL_TABS_IN_CURRENT_WINDOW') + ` (${groupedTabs.all.length})`,
  })
  browser.contextMenus.update('STORE_SELECTED_TABS', {
    title: __('menu_STORE_SELECTED_TABS') + ` (${groupedTabs.inter.length})`,
  })
  const lists = await storage.getLists()
  for (let i = 0; i < lists.length; i += 1) {
    if (!lists[i].title) continue
    browser.contextMenus.update('STORE_TO_TITLED_LIST.STORE_LEFT_TABS|' + i, {
      enabled: groupedTabs.left.length !== 0,
      title: __('menu_STORE_LEFT_TABS') + ` (${groupedTabs.left.length})`,
    })
    browser.contextMenus.update('STORE_TO_TITLED_LIST.STORE_RIGHT_TABS|' + i, {
      enabled: groupedTabs.right.length !== 0,
      title: __('menu_STORE_RIGHT_TABS') + ` (${groupedTabs.right.length})`,
    })
github dessant / search-by-image / src / background / main.js View on Github external
async function setContextMenu({removeFirst = false} = {}) {
  if (targetEnv === 'firefox' && (await isAndroid())) {
    return;
  }
  if (removeFirst) {
    await browser.contextMenus.removeAll();
  }
  const options = await storage.get(optionKeys, 'sync');
  const hasListener = browser.contextMenus.onClicked.hasListener(
    onContextMenuItemClick
  );
  if (options.showInContextMenu === true) {
    if (!hasListener) {
      browser.contextMenus.onClicked.addListener(onContextMenuItemClick);
    }
    await createMenu(options);
  } else {
    if (hasListener) {
      browser.contextMenus.onClicked.removeListener(onContextMenuItemClick);
    }
  }
}
github dessant / web-archives / src / background / main.js View on Github external
async function setContextMenu(removeFirst = false) {
  if (targetEnv === 'firefox' && (await isAndroid())) {
    return;
  }
  if (removeFirst) {
    await browser.contextMenus.removeAll();
  }
  const options = await storage.get(optionKeys, 'sync');
  const hasListener = browser.contextMenus.onClicked.hasListener(
    onContextMenuItemClick
  );
  if (options.showInContextMenu !== 'false') {
    if (!hasListener) {
      browser.contextMenus.onClicked.addListener(onContextMenuItemClick);
    }
    await createMenu(options);
  } else {
    if (hasListener) {
      browser.contextMenus.onClicked.removeListener(onContextMenuItemClick);
    }
  }
}
github cnwangjie / better-onetab / src / background.js View on Github external
enabled: groupedTabs.twoSide.length !== 0,
    title: __('menu_STORE_TWOSIDE_TABS') + ` (${groupedTabs.twoSide.length})`,
  })
  browser.contextMenus.update('STORE.STORE_ALL_TABS_IN_ALL_WINDOWS', {
    enabled: windows.length > 1,
  })
  browser.contextMenus.update('STORE.STORE_ALL_TABS_IN_CURRENT_WINDOW', {
    title: __('menu_STORE_ALL_TABS_IN_CURRENT_WINDOW') + ` (${groupedTabs.all.length})`,
  })
  browser.contextMenus.update('STORE_SELECTED_TABS', {
    title: __('menu_STORE_SELECTED_TABS') + ` (${groupedTabs.inter.length})`,
  })
  const lists = await storage.getLists()
  for (let i = 0; i < lists.length; i += 1) {
    if (!lists[i].title) continue
    browser.contextMenus.update('STORE_TO_TITLED_LIST.STORE_LEFT_TABS|' + i, {
      enabled: groupedTabs.left.length !== 0,
      title: __('menu_STORE_LEFT_TABS') + ` (${groupedTabs.left.length})`,
    })
    browser.contextMenus.update('STORE_TO_TITLED_LIST.STORE_RIGHT_TABS|' + i, {
      enabled: groupedTabs.right.length !== 0,
      title: __('menu_STORE_RIGHT_TABS') + ` (${groupedTabs.right.length})`,
    })
    browser.contextMenus.update('STORE_TO_TITLED_LIST.STORE_TWOSIDE_TABS|' + i, {
      enabled: groupedTabs.twoSide.length !== 0,
      title: __('menu_STORE_TWOSIDE_TABS') + ` (${groupedTabs.twoSide.length})`,
    })
    browser.contextMenus.update('STORE_TO_TITLED_LIST.STORE_ALL_TABS_IN_CURRENT_WINDOW|' + i, {
      title: __('menu_STORE_ALL_TABS_IN_CURRENT_WINDOW') + ` (${groupedTabs.all.length})`,
    })
    browser.contextMenus.update('STORE_TO_TITLED_LIST.STORE_SELECTED_TABS|' + i, {
      title: __('menu_STORE_SELECTED_TABS') + ` (${groupedTabs.inter.length})`,
github web-scrobbler / web-scrobbler / src / core / background / extension.js View on Github external
function updateContextMenu(tabId) {
		browser.contextMenus.removeAll();

		let controller = tabControllers[tabId];
		if (!controller) {
			return;
		}
		let connector = controller.getConnector();

		if (controller.isEnabled) {
			let title1 = browser.i18n.getMessage('menuDisableConnector', connector.label);
			addContextMenuItem(title1, () => {
				setConnectorState(controller, false);
				updateContextMenu(tabId);
			});

			let title2 = browser.i18n.getMessage('menuDisableUntilTabClosed');
			addContextMenuItem(title2, () => {
github kumabook / stickynotes / src / background.js View on Github external
function setupContextMenus() {
  browser.contextMenus.create({
    id:       'create-sticky',
    title:    'create sticky',
    contexts: ['all'],
  });

  browser.contextMenus.create({
    id:       'toggle-visibility',
    type:     'radio',
    title:    'show/hide stickies',
    contexts: ['all'],
  });

  browser.contextMenus.create({
    id:       'delete-all',
    title:    'delete all stickies on this page',
    contexts: ['all'],