📜 ⬆️ ⬇️

Interaction between web applications via Web Intents

Note: I usually do not translate, but the news is interesting.

In today's browser-based ecosystem, web applications are completely isolated from each other, and, for example, to send a comment to Twitter via a third-party service from your site, you need to use complex ( Note: well, I would just say “diverse” ) API . And what if we could give sites the opportunity to use such services without knowing what specific service we will use, but only knowing that it provides some functionality.

In Android OS, this problem is solved using Intents [1] - an architecture that allows components of one or several different applications to interact. According to this scheme, the client application creates a request (for example, send something) and sets the parameters that will be transferred to another service application. After that, the user is presented with a list of applications that have registered their ability to process this type of intent request. Then the application selected by the user is launched in the new context, and the parameters transmitted by the client in a format predetermined for a specific type of intent are transferred to it.
')
At the moment we are making great efforts to create a similar system for the web: Web Intents . This system will provide the same amenities as Intents for Android, but will be adapted for web applications.


We consider the development of a simple and easy-to-use API as our main and most important task. With Web Intents, you can connect the web application to the required service using just two lines of code! Chome does the rest for you.

As in Android, Web Intents will cover a basic set of actions (such as editing, viewing, etc.) that should satisfy most of the needs of modern sites. However, with the growth of the world wide web, sites offer more and more functionality. And therefore, they will be given the opportunity to create new types of Intent, which they will be able to publish and document themselves. We also plan to create a special site where you can view existing and new intents.

Consider as an example a site that is hosting photos, whose developers are not able to afford to add an online image editor to the site, but they understand that without this feature, the service will not become popular. The Web Intent system will allow them to get what they want with a minimum of effort:

var intent = new Intent(Intent.EDIT, 'image/png', getImageDataURI()); window.navigator.startActivity(intent, loadEditedImage); // This callback will be called when the service replies with the edited // image data. function loadEditedImage(data) { var image = document.getElementById('image'); setImageData(image, data); } 


Any site, for example, memes generator (note: well, or demotivators, if you wish) , which the user visits, will be able to announce the ability to process intent of type EDIT (editing) for files with image / * type using the following declaration:

 <intent action="http://webintents.org/edit" type="image/*" /> 


When a user needs to edit an image, this service will be offered to the user along with other registered image editors. And if the user selects a meme generator, in the new context the corresponding site will open, which will be able to get data from intent in the following way:

 var intent = window.intent; memeImg.src = intent.data; memegenForm.onsubmit = function() { // Transform the image - meme it. addMemeTaglines(memeImg, memeTopText, memeBottomText); // Send the generated meme back to the client. intent.postResult(getImageData(memeImg)); }; 


After calling postResult (), the context created for the meme generator will be closed, and the received data will be transferred to our application via the callback passed to startActivity (). (Note: in this example, loadEditedImage)

The guys from Mozilla also work in this direction . Moreover, we work closely with their engineers to create a single, simple and useful API.

We offer you to try a new feature in any modern browser on the page with examples . Those who want to try the Intents API on their website can use this patch for browsers that do not support this feature.

We quickly prototype this feature, so check your back soon for an announcement when Web Intents becomes available behind the flag in Chrome. We are working intensively on a prototype, so wait for the announcement when Web Intents will become an option in Chome, in the near future!

- [1] - intent - literally “intention”. Write in the comments how best to translate this term ... if at all it needs to be translated

Source: https://habr.com/ru/post/125772/


All Articles