Skip to content

Instantly share code, notes, and snippets.

@Zegnat
Created January 6, 2021 16:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Zegnat/8a96c343fcc9a83f0c12b63e1833292f to your computer and use it in GitHub Desktop.
Swapping between two browser context menus depending on the amount of selected tabs.
let single = true
const menus = {
single: browser.menus.create({
contexts: ['tab'],
title: '&Close Tab'
}),
selection: browser.menus.create({
contexts: ['tab'],
title: '&Close Tabs',
visible: false
})
}
browser.menus.onShown.addListener(async (info, tab) => {
if (!info.contexts.includes('tab')) return
if (
(!tab.active && tab.highlighted === single) ||
(tab.active && (await browser.tabs.query({
windowId: tab.windowId, highlighted: true
})).length === 1 !== single)
) {
single = !single
browser.menus.update(menus.single, { visible: single })
browser.menus.update(menus.selection, { visible: !single })
browser.menus.refresh()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment