Created
January 6, 2021 16:57
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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