Skip to content

Instantly share code, notes, and snippets.

@Rob--W
Created October 14, 2018 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rob--W/638057716d5d69d793193c5cd2bea0be to your computer and use it in GitHub Desktop.
Save Rob--W/638057716d5d69d793193c5cd2bea0be to your computer and use it in GitHub Desktop.
Feature-detect availability of viewTypes property in browser.menus API
// Example: How to feature-detect a new property (viewTypes) in the menus API
// Feature introduced in Firefox 64.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1416839
// // Minimal usage example:
// let menuCreateParams = {
// title: "some menu item meant for the sidebar",
// viewTypes: ["sidebar"],
// };
// if (!supportsMenuViewTypes()) {
// // Not supported, fall back to showing the menu everywhere?
// delete menuCreateParams.viewTypes;
// }
// browser.menus.create(menuCreateParams);
// Note: If used more than once, cache the result by storing it in a variable.
function supportsMenuViewTypes() {
try {
// The validation of API parameters in the WebExtensions API is often done
// synchronously (optionally followed by extra asynchronous validation).
// When an unsupported property is passed, an error is thrown.
// This characteristic can be used to perform feature detection rather
// easily: Call the method with a parameter and check if the call throws.
// In the example below, we use browser.menus.update with a non-existing
// menu ID, because such calls with non-existing menu IDs are silently
// ignored, so this feature detection method has no side effects.
browser.menus.update("unique ID that is not used for a real menu", {
// Just checking whether the property is supported.
// Optionally, you can also add an explicit value such as ["sidebar"]
// to see if that specific value is supported.
viewTypes: [],
});
return true;
} catch (e) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment