Dart Documentationchrome.contextMenusChromeContextMenus

ChromeContextMenus class

class ChromeContextMenus extends ChromeApi {
 static final JsObject _contextMenus = chrome['contextMenus'];

 ChromeContextMenus._();

 bool get available => _contextMenus != null;

 /**
  * Creates a new context menu item. Note that if an error occurs during
  * creation, you may not find out until the creation callback fires (the
  * details will be in chrome.runtime.lastError).
  * 
  * [callback] Called when the item has been created in the browser. If there
  * were any problems creating the item, details will be available in
  * chrome.runtime.lastError.
  * 
  * Returns:
  * The ID of the newly created item.
  */
 dynamic create(ContextMenusCreateParams createProperties, [dynamic callback]) {
   if (_contextMenus == null) _throwNotAvailable();

   return _contextMenus.callMethod('create', [jsify(createProperties), jsify(callback)]);
 }

 /**
  * Updates a previously created context menu item.
  * 
  * [id] The ID of the item to update.
  * 
  * [updateProperties] The properties to update. Accepts the same values as the
  * create function.
  */
 Future update(dynamic id, ContextMenusUpdateParams updateProperties) {
   if (_contextMenus == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _contextMenus.callMethod('update', [jsify(id), jsify(updateProperties), completer.callback]);
   return completer.future;
 }

 /**
  * Removes a context menu item.
  * 
  * [menuItemId] The ID of the context menu item to remove.
  */
 Future remove(dynamic menuItemId) {
   if (_contextMenus == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _contextMenus.callMethod('remove', [jsify(menuItemId), completer.callback]);
   return completer.future;
 }

 /**
  * Removes all context menu items added by this extension.
  */
 Future removeAll() {
   if (_contextMenus == null) _throwNotAvailable();

   var completer = new ChromeCompleter.noArgs();
   _contextMenus.callMethod('removeAll', [completer.callback]);
   return completer.future;
 }

 /**
  * Fired when a context menu item is clicked.
  */
 Stream<OnClickedEvent> get onClicked => _onClicked.stream;

 final ChromeStreamController<OnClickedEvent> _onClicked =
     new ChromeStreamController<OnClickedEvent>.twoArgs(_contextMenus, 'onClicked', _createOnClickedEvent);

 void _throwNotAvailable() {
   throw new UnsupportedError("'chrome.contextMenus' is not available");
 }
}

Extends

ChromeApi > ChromeContextMenus

Properties

final bool available #

Returns true if the API is available. The common causes of an API not being avilable are:

  • a permission is missing in the application's manifest.json file
  • the API is defined on a newer version of Chrome then the current runtime
docs inherited from ChromeApi
bool get available => _contextMenus != null;

final Stream<OnClickedEvent> onClicked #

Fired when a context menu item is clicked.

Stream<OnClickedEvent> get onClicked => _onClicked.stream;

Methods

dynamic create(ContextMenusCreateParams createProperties, [callback]) #

Creates a new context menu item. Note that if an error occurs during creation, you may not find out until the creation callback fires (the details will be in chrome.runtime.lastError).

callback Called when the item has been created in the browser. If there were any problems creating the item, details will be available in chrome.runtime.lastError.

Returns: The ID of the newly created item.

dynamic create(ContextMenusCreateParams createProperties, [dynamic callback]) {
 if (_contextMenus == null) _throwNotAvailable();

 return _contextMenus.callMethod('create', [jsify(createProperties), jsify(callback)]);
}

Future remove(menuItemId) #

Removes a context menu item.

menuItemId The ID of the context menu item to remove.

Future remove(dynamic menuItemId) {
 if (_contextMenus == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _contextMenus.callMethod('remove', [jsify(menuItemId), completer.callback]);
 return completer.future;
}

Future removeAll() #

Removes all context menu items added by this extension.

Future removeAll() {
 if (_contextMenus == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _contextMenus.callMethod('removeAll', [completer.callback]);
 return completer.future;
}

Future update(id, ContextMenusUpdateParams updateProperties) #

Updates a previously created context menu item.

id The ID of the item to update.

updateProperties The properties to update. Accepts the same values as the create function.

Future update(dynamic id, ContextMenusUpdateParams updateProperties) {
 if (_contextMenus == null) _throwNotAvailable();

 var completer = new ChromeCompleter.noArgs();
 _contextMenus.callMethod('update', [jsify(id), jsify(updateProperties), completer.callback]);
 return completer.future;
}