W3C home > Mailing lists > Public > public-webapps@w3.org > January to March 2015

[clipboard] Feature detect Clipboard API support?

From: James M. Greene <james.m.greene@gmail.com>
Date: Wed, 11 Feb 2015 12:21:19 -0600
Message-ID: <CALrbKZguzaZWdpP8b+a3jp0wGSNV=jbosd2b=ZbcAFzLNHW7=Q@mail.gmail.com>
To: WebApps WG <public-webapps@w3.org>
The current spec still leaves me a bit unclear about if implementors must
include the ability to feature detect Clipboard API support, which I think
is a critical requirement.

In particular, I *need* to be able to detect support for the Clipboard API
(click-to-copy support, in particular) in advance and without the user's
interaction in order to know if I need to load a Flash fallback or not.

If this is even *possible* based on the current spec, the only way I can
see that might make that possible is if executing `
document.execCommand("copy")` synthetically (without user interaction) MUST
still fire the `beforecopy`/`copy` events [but obviously not execute the
associated default action since it must not be authorized to inject into
the clipboard].  However, I don't feel that the spec defines any clear
stance on that.

Example detection attempt (more verbose version on JSBin [1]):

var execResult,
    isSupported = false;

if (typeof window.ClipboardEvent !== "undefined" && window.ClipboardEvent) {
  var checkSupport = function(e) {
    isSupported = !!e && e.type === "copy" && !!e.clipboardData && e
instanceof window.ClipboardEvent;
    document.removeEventListener("copy", checkSupport, false);
  document.addEventListener("copy", checkSupport, false);

  try {
    execResult = document.execCommand("copy");
  catch (e) {
    execResult = e;

  document.removeEventListener("copy", checkSupport, false);

  // Should I care about the `execResult` value for feature testing?
  // I don't think so.

  if (!isSupported) {
    // Fallback to Flash clipboard usage

This currently yields poor results, as well as arguably false positives for
`window.ClipboardEvent` (conforming to an earlier version of the spec,
perhaps?) in Firefox 22+ (22-35, currently) and pre-Blink Opera (<15).

It also causes security dialogs to popup in IE9-11 when invoking `
document.execCommand("copy")` if you do not first verify that
`window.ClipboardEvent` is present. That is obviously harmful to user

Can we agree upon some consistent feature detection technique to add to the
spec that can be guaranteed to yield correct results?  I would love it if
it were as simple as verifying that `window.ClipboardEvent` existed but, as
I mentioned, that is already yielding false positives today.

[1]: http://jsbin.com/davoxa/edit?html,js,output

    James Greene
Received on Wednesday, 11 February 2015 18:22:08 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 18:14:43 UTC