Hacker News new | ask | show | jobs
by sergiotapia 3738 days ago
Programmatic cut and copy to the clipboard It’s now possible to programmatically copy and cut text in response to a user gesture with document.execCommand('copy') and document.execCommand('cut'). Having this ability may eliminate some websites’ last need for the Flash plug-in.

---

:'( - So much pain erased in a single stroke!

4 comments

Unfortunately as far as I know the text selection API is still broken on mobile Safari. At least as of a few weeks ago the JS events associated with force touch seem to randomly wipe out the text selection, so even if you can now theoretically copy stuff to the clipboard it's largely irrelevant if users can't actually select text in the first place.
Can you file a bug with a specific example or two? http://bugs.webkit.org/
I think my cofounder already did, I'll check.

The basic issue we were encountering is that when selecting text via force touching and then dragging the selection, the text selection object was empty. (Though you can still hit Copy in the Safari UI.)

There wasn't any issue when selecting text via regular press or long press.

If you throw me a bug number (here or privately) I'll take a look.
OK the bug number is 25465034. Thanks!
That sucks. But (Desktop) Chrome/webkit selection has been partly broken too for a few years now.

https://bugs.webkit.org/show_bug.cgi?id=66630

https://bugs.chromium.org/p/chromium/issues/detail?id=346613

I've been dealing with this stuff for the last week.

Edit: added webkit reference

I do wonder why they didn't go with an API that other browsers are already supporting, though:

https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEv...

Other browsers support the execCommand API as well. Adding a new, arguably cleaner API is a separate undertaking from removing the unnecessary limitation on the older API.
Did you consider reading the page you linked to?

Clipboard events are already supported by safari (which the page you've linked to notes) and they've got nothing to do with execCommand, they are about hooking and reacting to user-triggered clipboard operations e.g. the user pastes (via contextual menu or shortcut), the software can know about it and alter the content before it's pasted.

execCommand is about programmatically triggering clipboard events e.g. the user clicks a button in the page and the application translates that to a copy or paste event.

Did you consider removing the first sentence of your post and it would have been less smug and taunting, leaving behind something that lifts up the conversation?
> Did you consider removing the first sentence of your post and it would have been less smug and taunting

Did you consider taking your own advice?

> leaving behind something that lifts up the conversation?

What are you talking about? There is no conversation, and what this subthread needs is not uplifting is euthanising.

It's probably only a matter of time but I worry that some advertisers copy ad links to my clipboard. With flash, I had to allow it at least...
I wouldn't ever enable clipboard integration -- a malicious site could use it to collect data by using the paste command with execCommand -- they'd grab whatever was on my clipboard, which could include my banking password from my password manager app that I was just getting ready to use.
That's why they only get the ability to programmatically trigger cut and copy, not paste.
Mozilla has it documented. Didn't see anything on the linked site.

https://developer.mozilla.org/en-US/docs/Web/API/Document/ex...

Firefox only makes execCommand("paste") available in trusted contexts aka extensions. You can't execCommand("paste") on a regular web page for fairly obvious security reasons.
I guess there's room for an attack of irritation where a user with something meaningful on their clipboard unwittingly triggers having it overwritten by some nonsense. (Most clipboards effectively being a single mutable item without history or stack/queue behaviour).
Tynt basically did this exact thing as a business model[0].

[0] http://daringfireball.net/2010/05/tynt_copy_paste_jerks

There's easier ways to irritate your users.

Most websites irritate their users, actually.

Speaking of clipboard shenanigans. Could be imagining it, but I seem to remember a while ago there being something certain websites were doing where if you selected and copied text from them, the text that ended up on your clipboard would have a synthesized paragraph about copyright/attribution appended to it.
Yeah, occured to me reading that comment. Lyrics websites used to do it a lot (probably still do). I believe they use invisible text.
Yeah, many tech news sites used to do this. I'm not positive, but I think TechCrunch did at one point, I could be wrong though. Absolutely hate it personally!
iBooks does exactly that; it irritates me to no end. I know why they do it, but still.
Is there programmatic access to the paste command? I only see cut and copy listed.
Paste is disallowed for just this reason.