fragmentioner
fragmentioner copied to clipboard
Fragmentioner default action should copy link instead of nav
Instead of "Link to Text", I want "Copy link to text" that copies the fragmention to the clipboard rather than navigates to it. Copying the link is a better default desired action than navigation. Why would you want to navigate to the thing you just selected? It's more likely you want to copy a link to what you just selected so you can post a highlight of, or comment on the selected text.
(Originally published at: http://tantek.com/2018/178/b1/fragmentioner-copy-instead-of-nav)
@tantek I have not found a good way of copying text to clipboard using vanilla JS. If you know a good documented way I would update the code.
Note: the execCommand("copy")
is deprecated https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#Browser_compatibility
<https://github.com/kartikprabhu> according to the linked examples on:
<https://indieweb.org/citation#append_to_text_selection_copy>
It looks like you can intercept document.oncopy and change the selection (using selection.selectAllChildren) to then cause the copy command itself to copy whatever text you want, rather than having to use execCommand("copy").
(Originally published at: http://tantek.com/2018/178/t1/interecept-document-oncopy-change-selection)
<https://github.com/kartikprabhu> Looks like you can also use the Clipboard API (<https://caniuse.com/clipboard>) to write directly to the clipboard (using navigator.clipboard.writeText) in response to e.g. a click on a link (or button), see and try the code in Example 10 here:
<https://w3c.github.io/clipboard-apis/#clipboard-writetext-data>
(Originally published at: http://tantek.com/2018/178/t2/clipboard-api-write-clipboard)
@tantek thanks for the links. I will look into it. My current thoughts are:
- if the Clipboard API is supported then the button should copy the link to clipboard
- else either
- do the current navigation UI, or
- display the link in a text box to copy manually
cc: @chrisaldrich @kaushalmodi since you also use fragmentioner
I agree that if it's possible to copy the fragmention link to the clipboard, that would be a better default. Thanks for copying me here.
Looks like the Clipboard API has not been implemented yet in FF. Also: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText#Browser_compatibility
Will look into hacking around with execCommand("copy")
for now and then switch over later.