Web Share API - Level 2

Draft Community Group Report

Latest editor's draft:
https://wicg.github.io/web-share/level-2/
Test suite:
https://w3c-test.org/web-share/
Editors:
Matt Giuca ( Google Inc. )
Eric Willigers ( Google Inc. )
Participate:
GitHub WICG/web-share
File a bug
Commit history
Pull requests
Implementation status:
Chromium

Abstract

This specification defines an API for sharing text, links and other content to an arbitrary destination of the user's choice.

The available share targets are not specified here; they are provided by the user agent. They could, for example, be apps, websites or contacts.

Status of This Document

This is a preview

Do not attempt to implement this version of the specification. Do not reference this version as authoritative in any way. Instead, see https://wicg.github.io/web-share/level-2/ for the Editor's draft.

This specification was published by the Web Incubator Community Group . It is not a W3C Standard nor is it on the W3C Standards Track. Please note that under the W3C Community Contributor License Agreement (CLA) there is a limited opt-out and other conditions apply. Learn more about W3C Community and Business Groups .

This is an early draft of the Web Share Level 2 spec. It it subject to review and changes.

1. Usage Examples

This section is non-normative.

This example shows a basic share operation. In response to a button click, this JavaScript code shares the current page's URL.

Example 1 : Basic usage
shareButton.addEventListener("click", async () => {
  try {
    await navigator.share({ title: "Example Page", url: "" });
    console.log("Data was shared successfully");
  } catch (err) {
    console.error("Share failed:", err.message);
  }
});

Note that a url of '' refers to the current page URL, just as it would in a link. Any other absolute or relative URL can also be used.

In response to this call to navigator.share() , the user agent would display a picker or chooser dialog, allowing the user to select a target to share this title and the page URL to.

2. API definition

2.2 ShareData dictionary

dictionary ShareData {
  USVString title;
  USVString text;
  USVString url;
  FrozenArray<File> files;
};

The ShareData dictionary consists of several optional members:

files member
A File array referring to files being shared.
text member
Arbitrary text that forms the body of the message being shared.
title member
The title of the document being shared. May be ignored by the target.
url member
A URL string referring to a resource being shared.
Note
The string members are USVString (as opposed to DOMString ) because they are not allowed to contain invalid UTF-16 surrogates. This means the user agent is free to re-encode them in any Unicode encoding (e.g., UTF-8 ).
Note
The url member can contain a relative URL . In this case, it will be automatically resolved relative to the current page location, just like a href on an a element, before being given to the share target.

3. Share targets

A share target is the abstract concept of a destination that the user agent will transmit the share data to. What constitutes a share target is at the discretion of the user agent.

A share target might not be directly able to accept a ShareData (due to not having been written with this API in mind). However, it MUST have the ability to receive data that matches some or all of the concepts exposed in ShareData . To convert data to a format suitable for ingestion into the target , the user agent SHOULD map the members of ShareData onto equivalent concepts in the target. It MAY discard members if necessary. The meaning of each member of the payload is at the discretion of the share target.

Each share target MAY be made conditionally available depending on the ShareData payload delivered to the share() method.

Note
Once a share target has been given the payload, the share is considered successful. If the target considers the data unacceptable or an error occurs, it can either recover gracefully, or show an error message to the end-user; it cannot rely on the sender to handle errors. In other words, the share() method is "fire and forget"; it does not wait for the target to approve or reject the payload.

3.1 Examples of share targets

This section is non-normative.

The list of share targets can be populated from a variety of sources, depending on the user agent and host operating system. For example:

Note
There is an attempt to standardize the registration of websites to receive share data for that final use case; see Web Share Target .

In some cases, the host operating system will provide a sharing or intent system similar to Web Share. In these cases, the user agent can simply forward the share data to the operating system and not talk directly to native applications.

Mapping the ShareData to the share target (or operating system)'s native format can be tricky as some platforms will not have an equivalent set of members. For example, if the target has a "text" member but not a "URL" member, one solution is to concatenate both the text and url members of ShareData and pass the result in the "text" member of the target.

4. Dependencies

The following are defined in [ WEBIDL ]:

TypeError is defined by [ ECMASCRIPT ].

5. Security and privacy considerations

This section is non-normative.

Web Share enables data to be sent from websites to native applications. While this ability is not unique to Web Share, it does come with a number of potential security issues that can vary in severity (depending on the underlying platform).

A. Extensibility of this API

This section is non-normative.

The Web Share API is designed to be extended in the future by way of new members added to the ShareData dictionary, to allow both sharing of new types of data ( e.g. , images ) and strings with new semantics ( e.g. author).

Warning
This doesn't mean user agents can add whatever members they like. It means that new members can be added to the standard in the future.

The three members title , text , and url , are part of the base feature set, and implementations that provide navigator.share() need to accept all three. Any new members that are added in the future will be individually feature-detectable , to allow for backwards-compatibility with older implementations that don't recognize those members. These new members might also be added as optional " MAY " requirements.

Note
There is an open discussion about how to provide feature-detection for dictionary members. Web Share will use the mechanism produced by that discussion.

The share() method returns a rejected promise with a TypeError if none of the specified members are present. The intention is that when a new member is added, it will also be added to this list of recognized members. This is for future-proofing implementations: if a web site written against a future version of this spec uses only new members ( e.g. , navigator.share({image: x}) ), it will be valid in future user agents, but a TypeError on user agents implementing an older version of the spec. Developers will be asked to feature-detect any new members they rely on, to avoid having errors surface in their program.

Editors of this spec will want to carefully consider the genericity of any new members being added, avoiding members that are closely associated with a particular service, user agent or operating system, in favour of members that can potentially be applied to a wide range of platforms and targets.

B. Acknowledgments

Thanks to the Web Intents team, who laid the groundwork for the web app interoperability use cases. In particular, Paul Kinlan , who did a lot of early advocacy for Web Share.

C. Changes since Level 1

D. References

D.1 Normative references

[ECMASCRIPT]
ECMAScript Language Specification . Ecma International. URL: https://tc39.github.io/ecma262/
[FILE-API]
File API . Marijn Kruisselbrink; Arun Ranganathan. W3C. 6 November 2018. W3C Working Draft. URL: https://www.w3.org/TR/FileAPI/
[HTML]
HTML Standard . Anne van Kesteren; Domenic Denicola; Ian Hickson; Philip Jägenstedt; Simon Pieters. WHATWG. Living Standard. URL: https://html.spec.whatwg.org/multipage/
[promises-guide]
Writing Promise-Using Specifications . Domenic Denicola. W3C. 16 February 2016. TAG Finding. URL: https://www.w3.org/2001/tag/doc/promises-guide
[URL]
URL Standard . Anne van Kesteren. WHATWG. Living Standard. URL: https://url.spec.whatwg.org/
[WEBIDL]
Web IDL . Cameron McCormack; Boris Zbarsky; Tobie Langel. W3C. 15 December 2016. W3C Editor's Draft. URL: https://heycam.github.io/webidl/

D.2 Informative references

[rfc2781]
UTF-16, an encoding of ISO 10646 . P. Hoffman; F. Yergeau. IETF. February 2000. Informational. URL: https://tools.ietf.org/html/rfc2781
[rfc3629]
UTF-8, a transformation format of ISO 10646 . F. Yergeau. IETF. November 2003. Internet Standard. URL: https://tools.ietf.org/html/rfc3629