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

Re: [clipboard] Feature detect Clipboard API support?

From: Michaela Merz <michaela.merz@hermetos.com>
Date: Wed, 11 Feb 2015 12:34:04 -0600
Message-ID: <54DBA09C.6020103@hermetos.com>
To: "James M. Greene" <james.m.greene@gmail.com>, WebApps WG <public-webapps@w3.org>

AFAIK, you can't trigger a clip board request without human interaction.

 $('#element).off().on('click',function(e)     {
    var clip = new ClipboardEvent('copy');
    clip.clipboardData.setData('text/plain','some data');
    clip.preventDefault();
    e.target.dispatchEvent(clip);
    });

This unfortunately won't work in my environment since my code is not
'trusted'.

m.

On 02/11/2015 12:21 PM, James M. Greene wrote:

> 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]):
> 
> ```js 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 experience.
> 
> 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
> 
> 
> 
> Sincerely, James Greene
Received on Wednesday, 11 February 2015 18:34:32 UTC

This archive was generated by hypermail 2.3.1 : Friday, 27 October 2017 07:27:25 UTC