Connect Apps with Web Intents

Warning: Deprecated in Chrome 24. Web intents are no longer supported.

Web Intents allow your application to quickly communicate with other applications on the user's system and inside their browser. Your application can register to handle specific user actions such as editing images via the manifest.json; your application can also invoke actions to be handled by other applications.

Chrome Apps use Web Intents as their primary mechanism for inter-app communication.

API Samples: Want to play with the code? Check out the webintents sample.

Register your app to handle an action

Warning: Deprecated in Chrome 24. Web intents are no longer supported.

You must supply the intent in the manifest:

"intents":{
 "http://webintents.org/edit" : [{
   "title" : "Best Image editing app",
   "type" : ["image/*"]
 }]
}

Unlike extensions and hosted apps, Chrome applications do not need a "href" attribute in the manifest declaration, this is because Chrome Apps have a single entry point for launch - the onLaunched event.

Handling content types

Warning: Deprecated in Chrome 24. Web intents are no longer supported.

Your application can be the user's preferred choice for handling a file type. For example, your application could handle viewing images or viewing pdfs. You must supply the intent in the manifest and use the "http://webintents.org/view" action:

To be able declare your application's ability to view RSS and ATOM feeds, you would add the following to your manifest.

"intents": {
 "http://webintents.org/view" : [{
   "title" : "RSS Feed Reader",
   "type" : ["application/atom+xml", "application/rss+xml"]
 }]
}

Your application will receive intent payload through the onLaunched event.

chrome.app.runtime.onLaunched(function(intent) {
  // App Launched
  if(intent.action == "http://webinents.org/view" &&
     intent.type == "application/atom+xml") {

    // obtain the ATOM feed data.
    var data = intent.data;
  }
});

Launching an app with a file

Warning: Deprecated in Chrome 24. Web intents are no longer supported.

If your app handles the view intent, it is possible to launch it from the command line with a file as a parameter.

chrome.exe --app-id [app_id] [path_to_file]

This will implicity launch your application with an intent payload populated with the action set to "http://webintents.org/view", the type set to the mime-type of the file and the data as a FileEntry object.

chrome.app.runtime.onLaunched(function(intent) {
  // App Launched
  var data = intent.data;
});

Manipulating the file

Warning: Deprecated in Chrome 24. Web intents are no longer supported.

When your application is launched with a file as the parameter on the command-line, the intent.data property is a FileEntry. This is really cool because now you have a direct reference back to the physical file on the disk, and you can write data back to it.

chrome.app.runtime.onLaunched(function(intent) {
  // App Launched
  var data = intent.data;
  if(data instanceof FileEntry) {
    data.createWriter(function(writer) {
      writer.onwriteend = function(e) {
        console.log('Write completed.');
      };

      writer.onerror = function(e) {
        console.log('Write failed: ' + e.toString());
      };

      // Create a new Blob and write it to log.txt.
      var blob = new Blob(['Lorem Ipsum'], {type: 'text/plain'});
      writer.write(blob);
    });
  }
});

Returning data to calling application

Warning: Deprecated in Chrome 24. Web intents are no longer supported.

Lots of applications want to cooperate with the app that invoked them. It's easy to send data back to the calling client using intent.postResult:

chrome.app.runtime.onLaunched(function(intent) {
  // App Launched
  console.log(intent.action);
  console.log(intent.type);
  var data = intent.data;
  // Do something with the data;

  intent.postResult(newData);
});

Localizing your app title

Warning: Deprecated in Chrome 24. Web intents are no longer supported.

If your application or extension is localized as per the guidelines in Internationalization (i18n), you can localize the title of your intent in the picker using the exact same infrastructure:

"intents": {
 "http://webintents.org/edit" : [{
   "title" : "__MSG_intent_title__",
   "type" : ["image/*"],
   "disposition" : "inline"
 }]
}

Invoking an action

Warning: Deprecated in Chrome 24. Web intents are no longer supported.

If your application needs to be able to use the functionality of another application, it can simply ask the browser for it. To ask for an application that supports image editing, it's as simple as:

var intent = new WebKitIntent("http://webintents.org/edit", "image/png", "dataUri://");

window.navigator.webkitStartActivity(intent, function(data) {
// The data from the remote application is returned here.
});

Handling Errors and Exceptions

Warning: Deprecated in Chrome 24. Web intents are no longer supported.

If your service application needs to signal to the client application that an unrecoverable error has occurred, then your application will need to call postError on the intent object. This will signal to the client’s onError callback that something has gone wrong.

Client

var intent = new WebKitIntent("http://webintents.org/edit", "image/png", "dataUri://");

var onSuccess = function(data) {};
var onError = function() {};

window.navigator.webkitStartActivity(intent, onSuccess, onError);

Service

chrome.app.runtime.onLaunched(function(intent) {
  // App Launched
  console.log(intent.action);
  console.log(intent.type);
  var data = intent.data;
  // Do something with the data;

  intent.postResult(newData);
});

Back to top