Simple Machines Community Forum

SMF Support => SMF 2.0.x Support => Topic started by: spiros on February 24, 2019, 07:06:13 AM

Title: Select and copy javascript
Post by: spiros on February 24, 2019, 07:06:13 AM
Currently, if one adds the code tag, the smfSelectText(this); bit allows to select the code. Could this be improved so that it copies too?
Title: Re: Select and copy javascript
Post by: Arantor on February 24, 2019, 07:42:21 AM
The clipboard API is very fussy about what it allows you to do because code being allowed to copy to the clipboard carries security implications. Most of the time this is offered by Flash based fallbacks, so you can see how useful that's going to be...
Title: Re: Select and copy javascript
Post by: spiros on February 24, 2019, 08:19:39 AM
Right, I know about the security implications, but I have found js only solutions that seem fairly decent:

https://hackernoon.com/copying-text-to-clipboard-with-javascript-df4d4988697f
https://clipboardjs.com/

See also:
https://stackoverflow.com/questions/127040/copy-put-text-on-the-clipboard-with-firefox-safari-and-chrome
https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript
Title: Re: Select and copy javascript
Post by: Arantor on February 24, 2019, 08:45:45 AM
If Google Docs can't do it consistently in a major browser and puts up a prompt telling people to use Ctrl-C on their keyboard (as in Firefox)... I doubt this really works as well as advertised.

Even putting that aside it cannot be added to smfSelectText in 2.0 as this is needed for other routines and one of the huge deals of the clipboard API is that you can't call it directly from a function but have to trigger it only in an event handler for security reasons.
Title: Re: Select and copy javascript
Post by: spiros on March 13, 2019, 07:13:00 AM
It works just fine in Adsense and http://goo.gl :)