1<h1>Connect Apps with Web Intents</h1> 2 3<p class="warning"> 4<b>Warning: </b> 5Deprecated in Chrome 24. 6Web intents are no longer supported. 7</p> 8 9<p> 10<a href="http://webintents.org/">Web Intents</a> 11allow your application to quickly communicate 12with other applications on the user's system and inside their browser. 13Your application can register to handle specific user actions 14such as editing images via the <code>manifest.json</code>; 15your application can also invoke actions to be handled by other applications. 16</p> 17 18<p>Chrome Apps use Web Intents as their primary mechanism for inter-app 19communication.</p> 20 21<p class="note"> 22<b>API Samples: </b> 23Want to play with the code? 24Check out the 25<a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/webintents">webintents</a> sample. 26</p> 27 28<h2 id="register">Register your app to handle an action</h2> 29 30<p class="warning"> 31<b>Warning: </b> 32Deprecated in Chrome 24. 33Web intents are no longer supported. 34</p> 35 36<p> 37You must supply the intent in the manifest: 38</p> 39 40<pre data-filename="manifest.json"> 41"intents":{ 42 "http://webintents.org/edit" : [{ 43 "title" : "Best Image editing app", 44 "type" : ["image/*"] 45 }] 46} 47</pre> 48 49<p> 50Unlike extensions and hosted apps, Chrome applications do not 51need a "href" attribute in the manifest declaration, this is 52because Chrome Apps have a single entry point for 53launch - the <code>onLaunched</code> event. 54</p> 55 56<h2 id="content">Handling content types</h2> 57 58<p class="warning"> 59<b>Warning: </b> 60Deprecated in Chrome 24. 61Web intents are no longer supported. 62</p> 63 64<p> 65Your application can be the user's preferred choice for handling a file type. 66For example, your application could handle viewing images or viewing pdfs. 67You must supply the intent in the manifest 68and use the "http://webintents.org/view" action: 69</p> 70<p>To be able declare your application's ability to view RSS and ATOM 71feeds, you would add the following to your manifest. 72</p> 73<pre data-filename="manifest.json"> 74"intents": { 75 "http://webintents.org/view" : [{ 76 "title" : "RSS Feed Reader", 77 "type" : ["application/atom+xml", "application/rss+xml"] 78 }] 79} 80</pre> 81 82<p> 83Your application will receive intent payload through the <code>onLaunched</code> event. 84</p> 85<pre> 86chrome.app.runtime.onLaunched(function(intent) { 87 // App Launched 88 if(intent.action == "http://webinents.org/view" && 89 intent.type == "application/atom+xml") { 90 91 // obtain the ATOM feed data. 92 var data = intent.data; 93 } 94}); 95</pre> 96 97<h2 id="launching">Launching an app with a file</h2> 98 99<p class="warning"> 100<b>Warning: </b> 101Deprecated in Chrome 24. 102Web intents are no longer supported. 103</p> 104 105<p> 106If your app handles the <code>view</code> intent, 107it is possible to launch it from the command line with a file as a parameter. 108</p> 109<pre> 110chrome.exe --app-id [app_id] [path_to_file] 111</pre> 112<p> 113This will implicity launch your application with an intent payload populated 114with the action set to "http://webintents.org/view", the type set to the 115mime-type of the file and the data as a <code>FileEntry</code> object. 116</p> 117<pre> 118chrome.app.runtime.onLaunched(function(intent) { 119 // App Launched 120 var data = intent.data; 121}); 122</pre> 123 124<h2 id="launching">Manipulating the file</h2> 125 126<p class="warning"> 127<b>Warning: </b> 128Deprecated in Chrome 24. 129Web intents are no longer supported. 130</p> 131 132<p> 133 When your application is launched with a file as the parameter 134 on the command-line, 135 the <code>intent.data</code> property is a <code>FileEntry</code>. 136 This is really cool because now you have a direct reference back to the physical 137 file on the disk, 138 and you can write data back to it. 139</p> 140 141<pre> 142chrome.app.runtime.onLaunched(function(intent) { 143 // App Launched 144 var data = intent.data; 145 if(data instanceof FileEntry) { 146 data.createWriter(function(writer) { 147 writer.onwriteend = function(e) { 148 console.log('Write completed.'); 149 }; 150 151 writer.onerror = function(e) { 152 console.log('Write failed: ' + e.toString()); 153 }; 154 155 // Create a new Blob and write it to log.txt. 156 var blob = new Blob(['Lorem Ipsum'], {type: 'text/plain'}); 157 writer.write(blob); 158 }); 159 } 160}); 161</pre> 162 163<h2 id="return">Returning data to calling application</h2> 164 165<p class="warning"> 166<b>Warning: </b> 167Deprecated in Chrome 24. 168Web intents are no longer supported. 169</p> 170 171<p> 172Lots of applications want to cooperate 173with the app that invoked them. 174It's easy to send data back to the calling client 175using <code>intent.postResult</code>: 176</p> 177 178<pre> 179chrome.app.runtime.onLaunched(function(intent) { 180 // App Launched 181 console.log(intent.action); 182 console.log(intent.type); 183 var data = intent.data; 184 // Do something with the data; 185 186 intent.postResult(newData); 187}); 188</pre> 189 190<h2 id="localize">Localizing your app title</h2> 191 192<p class="warning"> 193<b>Warning: </b> 194Deprecated in Chrome 24. 195Web intents are no longer supported. 196</p> 197 198<p> 199If your application or extension is localized 200as per the guidelines in 201<a href="i18n">Internationalization (i18n)</a>, 202you can localize the title of your intent in the picker 203using the exact same infrastructure: 204</p> 205 206<pre data-filename="manifest.json"> 207"intents": { 208 "http://webintents.org/edit" : [{ 209 "title" : "__MSG_intent_title__", 210 "type" : ["image/*"], 211 "disposition" : "inline" 212 }] 213} 214</pre> 215 216<h2 id="invoke">Invoking an action</h2> 217 218<p class="warning"> 219<b>Warning: </b> 220Deprecated in Chrome 24. 221Web intents are no longer supported. 222</p> 223 224<p> 225If your application needs to be able 226to use the functionality of another application, 227it can simply ask the browser for it. 228To ask for an application that supports image editing, 229it's as simple as: 230</p> 231 232<pre> 233var intent = new WebKitIntent("http://webintents.org/edit", "image/png", "dataUri://"); 234 235window.navigator.webkitStartActivity(intent, function(data) { 236// The data from the remote application is returned here. 237}); 238</pre> 239 240<h2 id="errors">Handling Errors and Exceptions</h2> 241 242<p class="warning"> 243<b>Warning: </b> 244Deprecated in Chrome 24. 245Web intents are no longer supported. 246</p> 247 248<p> 249 If your service application needs to signal to the client application 250 that an unrecoverable error has occurred, 251 then your application will need 252 to call <code>postError</code> on the intent object. 253 This will signal to the client’s onError callback 254 that something has gone wrong. 255</p> 256 257<h3 id="client">Client</h3> 258 259<pre> 260var intent = new WebKitIntent("http://webintents.org/edit", "image/png", "dataUri://"); 261 262var onSuccess = function(data) {}; 263var onError = function() {}; 264 265window.navigator.webkitStartActivity(intent, onSuccess, onError); 266</pre> 267 268<h3 id="service">Service</h3> 269<pre> 270chrome.app.runtime.onLaunched(function(intent) { 271 // App Launched 272 console.log(intent.action); 273 console.log(intent.type); 274 var data = intent.data; 275 // Do something with the data; 276 277 intent.postResult(newData); 278}); 279</pre> 280 281<p class="backtotop"><a href="#top">Back to top</a></p> 282