Dart Documentationchrome.extensionChromeExtension

ChromeExtension class

class ChromeExtension extends ChromeApi {
 static final JsObject _extension = chrome['extension'];

 ChromeExtension._();

 bool get available => _extension != null;

 /**
  * Set for the lifetime of a callback if an ansychronous extension api has
  * resulted in an error. If no error has occured lastError will be
  * [undefined].
  */
 LastErrorExtension get lastError => _createLastErrorExtension(_extension['lastError']);

 /**
  * True for content scripts running inside incognito tabs, and for extension
  * pages running inside an incognito process. The latter only applies to
  * extensions with 'split' incognito_behavior.
  */
 bool get inIncognitoContext => _extension['inIncognitoContext'];

 /**
  * Deprecated: Please use sendMessage.
  * 
  * [extensionId] The extension ID of the extension you want to connect to. If
  * omitted, default is your own extension.
  * 
  * Returns:
  * The JSON response object sent by the handler of the request. If an error
  * occurs while connecting to the extension, the callback will be called with
  * no arguments and [runtime.lastError] will be set to the error message.
  */
 Future<dynamic> sendRequest(dynamic request, [String extensionId]) {
   if (_extension == null) _throwNotAvailable();

   var completer = new ChromeCompleter<dynamic>.oneArg();
   _extension.callMethod('sendRequest', [extensionId, jsify(request), completer.callback]);
   return completer.future;
 }

 /**
  * Converts a relative path within an extension install directory to a
  * fully-qualified URL.
  * 
  * [path] A path to a resource within an extension expressed relative to its
  * install directory.
  * 
  * Returns:
  * The fully-qualified URL to the resource.
  */
 String getURL(String path) {
   if (_extension == null) _throwNotAvailable();

   return _extension.callMethod('getURL', [path]);
 }

 /**
  * Returns an array of the JavaScript 'window' objects for each of the pages
  * running inside the current extension.
  * 
  * Returns:
  * Array of global objects
  */
 List<Window> getViews([ExtensionGetViewsParams fetchProperties]) {
   if (_extension == null) _throwNotAvailable();

   var ret = _extension.callMethod('getViews', [jsify(fetchProperties)]);
   return ret;
 }

 /**
  * Returns the JavaScript 'window' object for the background page running
  * inside the current extension. Returns null if the extension has no
  * background page.
  */
 Window getBackgroundPage() {
   if (_extension == null) _throwNotAvailable();

   return _createWindow(_extension.callMethod('getBackgroundPage'));
 }

 /**
  * Deprecated. Please use getViews({type: 'TAB'}). Returns an array of the
  * JavaScript 'window' objects for each of the tabs running inside the current
  * extension. If windowId is specified, returns only the 'window' objects of
  * tabs attached to the specified window.
  * 
  * Returns:
  * Array of global window objects
  */
 List<Window> getExtensionTabs([int windowId]) {
   if (_extension == null) _throwNotAvailable();

   var ret = _extension.callMethod('getExtensionTabs', [windowId]);
   return ret;
 }

 /**
  * Retrieves the state of the extension's access to Incognito-mode (as
  * determined by the user-controlled 'Allowed in Incognito' checkbox.
  * 
  * Returns:
  * True if the extension has access to Incognito mode, false otherwise.
  */
 Future<bool> isAllowedIncognitoAccess() {
   if (_extension == null) _throwNotAvailable();

   var completer = new ChromeCompleter<bool>.oneArg();
   _extension.callMethod('isAllowedIncognitoAccess', [completer.callback]);
   return completer.future;
 }

 /**
  * Retrieves the state of the extension's access to the 'file://' scheme (as
  * determined by the user-controlled 'Allow access to File URLs' checkbox.
  * 
  * Returns:
  * True if the extension can access the 'file://' scheme, false otherwise.
  */
 Future<bool> isAllowedFileSchemeAccess() {
   if (_extension == null) _throwNotAvailable();

   var completer = new ChromeCompleter<bool>.oneArg();
   _extension.callMethod('isAllowedFileSchemeAccess', [completer.callback]);
   return completer.future;
 }

 /**
  * Sets the value of the ap CGI parameter used in the extension's update URL.
  * This value is ignored for extensions that are hosted in the Chrome
  * Extension Gallery.
  */
 void setUpdateUrlData(String data) {
   if (_extension == null) _throwNotAvailable();

   _extension.callMethod('setUpdateUrlData', [data]);
 }

 /**
  * Deprecated: please use onMessage.
  */
 Stream<OnRequestEvent> get onRequest => _onRequest.stream;

 final ChromeStreamController<OnRequestEvent> _onRequest =
     new ChromeStreamController<OnRequestEvent>.threeArgs(_extension, 'onRequest', _createOnRequestEvent);

 /**
  * Deprecated: please use onMessageExternal.
  */
 Stream<OnRequestExternalEvent> get onRequestExternal => _onRequestExternal.stream;

 final ChromeStreamController<OnRequestExternalEvent> _onRequestExternal =
     new ChromeStreamController<OnRequestExternalEvent>.threeArgs(_extension, 'onRequestExternal', _createOnRequestExternalEvent);

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

Extends

ChromeApi > ChromeExtension

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 => _extension != null;

final bool inIncognitoContext #

True for content scripts running inside incognito tabs, and for extension pages running inside an incognito process. The latter only applies to extensions with 'split' incognito_behavior.

bool get inIncognitoContext => _extension['inIncognitoContext'];

final LastErrorExtension lastError #

Set for the lifetime of a callback if an ansychronous extension api has resulted in an error. If no error has occured lastError will be undefined.

LastErrorExtension get lastError => _createLastErrorExtension(_extension['lastError']);

final Stream<OnRequestEvent> onRequest #

Deprecated: please use onMessage.

Stream<OnRequestEvent> get onRequest => _onRequest.stream;

final Stream<OnRequestExternalEvent> onRequestExternal #

Deprecated: please use onMessageExternal.

Stream<OnRequestExternalEvent> get onRequestExternal => _onRequestExternal.stream;

Methods

Window getBackgroundPage() #

Returns the JavaScript 'window' object for the background page running inside the current extension. Returns null if the extension has no background page.

Window getBackgroundPage() {
 if (_extension == null) _throwNotAvailable();

 return _createWindow(_extension.callMethod('getBackgroundPage'));
}

List<Window> getExtensionTabs([int windowId]) #

Deprecated. Please use getViews({type: 'TAB'}). Returns an array of the JavaScript 'window' objects for each of the tabs running inside the current extension. If windowId is specified, returns only the 'window' objects of tabs attached to the specified window.

Returns: Array of global window objects

List<Window> getExtensionTabs([int windowId]) {
 if (_extension == null) _throwNotAvailable();

 var ret = _extension.callMethod('getExtensionTabs', [windowId]);
 return ret;
}

String getURL(String path) #

Converts a relative path within an extension install directory to a fully-qualified URL.

path A path to a resource within an extension expressed relative to its install directory.

Returns: The fully-qualified URL to the resource.

String getURL(String path) {
 if (_extension == null) _throwNotAvailable();

 return _extension.callMethod('getURL', [path]);
}

List<Window> getViews([ExtensionGetViewsParams fetchProperties]) #

Returns an array of the JavaScript 'window' objects for each of the pages running inside the current extension.

Returns: Array of global objects

List<Window> getViews([ExtensionGetViewsParams fetchProperties]) {
 if (_extension == null) _throwNotAvailable();

 var ret = _extension.callMethod('getViews', [jsify(fetchProperties)]);
 return ret;
}

Future<bool> isAllowedFileSchemeAccess() #

Retrieves the state of the extension's access to the 'file://' scheme (as determined by the user-controlled 'Allow access to File URLs' checkbox.

Returns: True if the extension can access the 'file://' scheme, false otherwise.

Future<bool> isAllowedFileSchemeAccess() {
 if (_extension == null) _throwNotAvailable();

 var completer = new ChromeCompleter<bool>.oneArg();
 _extension.callMethod('isAllowedFileSchemeAccess', [completer.callback]);
 return completer.future;
}

Future<bool> isAllowedIncognitoAccess() #

Retrieves the state of the extension's access to Incognito-mode (as determined by the user-controlled 'Allowed in Incognito' checkbox.

Returns: True if the extension has access to Incognito mode, false otherwise.

Future<bool> isAllowedIncognitoAccess() {
 if (_extension == null) _throwNotAvailable();

 var completer = new ChromeCompleter<bool>.oneArg();
 _extension.callMethod('isAllowedIncognitoAccess', [completer.callback]);
 return completer.future;
}

Future<dynamic> sendRequest(request, [String extensionId]) #

Deprecated: Please use sendMessage.

extensionId The extension ID of the extension you want to connect to. If omitted, default is your own extension.

Returns: The JSON response object sent by the handler of the request. If an error occurs while connecting to the extension, the callback will be called with no arguments and runtime.lastError will be set to the error message.

Future<dynamic> sendRequest(dynamic request, [String extensionId]) {
 if (_extension == null) _throwNotAvailable();

 var completer = new ChromeCompleter<dynamic>.oneArg();
 _extension.callMethod('sendRequest', [extensionId, jsify(request), completer.callback]);
 return completer.future;
}

void setUpdateUrlData(String data) #

Sets the value of the ap CGI parameter used in the extension's update URL. This value is ignored for extensions that are hosted in the Chrome Extension Gallery.

void setUpdateUrlData(String data) {
 if (_extension == null) _throwNotAvailable();

 _extension.callMethod('setUpdateUrlData', [data]);
}