30

Wouldn't it be cool to right click anywhere in a code block and have the option "copy code block to clipboard"?

  • How would this even be possible on a web site? – Will Robertson Jan 9 '11 at 1:53
  • With firebug on firefox, I can right click on a code block and do "inspect element". This is an element that starts like this: <pre class="lang-tex prettyprint"> Now if I could strip out all the HTML tags, I would have my code block. This is probably a long way round, but does this not show this is possible? – Seamus Jan 9 '11 at 15:37
  • 5
    For security/privacy reasons, most browsers don't seem to allow javascript to access the clipboard. There are workarounds (see SO), but they all use flash. On the other hand, javascript run via greasemonkey can access the clipboard. – Caramdir Jan 9 '11 at 17:35
  • 7
    Perhaps at least 'select this code block' for copying? This is seen on other sites (for example latex-community.org) – Joseph Wright Jan 9 '11 at 17:36
  • Yeah. Select the code for copying works just as well, and looks like it might be easier to implement. – Seamus Jan 9 '11 at 21:00
  • @Seamus: my point is that right-clicking is surely an OS and browser-specific function. I'm not aware of any server-side approaches to affect what appears when I right-click on a website. (Could be wrong, though.) A button for selecting code for copying would be okay, though. (Flash can definitely copy text to the clipboard—see GitHub—but I'm not about to suggest it for this site!) – Will Robertson Jan 11 '11 at 2:15
  • @Will Oh I see what you mean. Well, right-clicking was an incidental part of the feature I'm really requesting, which is a quick way to highlight/copy a whole code block... – Seamus Jan 11 '11 at 11:24
21

I now programmed this Greasemonkey script for Firefox to add a 'select code' text below every pre tag. If clicked the tag content will be selected and the user can copy it using CTRL+C or similar.

This is not a full solution yet (only work with Firefox) but could be expanded to all browsers and included in the site javascript files.

It looks like this:

enter image description here

  • 3
    Please make it work also with Chrome. – lockstep Feb 19 '11 at 16:11
  • @lockstep: I will have a look on it – Martin Scharrer Feb 19 '11 at 19:10
  • I've linked to this answer on meta.gis.stackexchange.com/questions/483/…, where a similar question has come up. The script works there (and I suspect on all stackexchange sites) if you use // @include http://*.stackexchange.com/*. Maybe you could incorporate that in the version hosted on your server? – Jake May 29 '11 at 20:47
  • @lockstep: Does it work with the current version of Chrome? – Martin Scharrer May 18 '12 at 8:42
  • @lockstep: It works now. But you need tempermonkey user script manager. Refer : How to make `select code` work with google chrome? – user11232 Jan 19 '13 at 13:47
  • @MartinScharrer -- this question has been cited in meta.tex.stackexchange.com/q/6183/579 but trying to connect to your link fails. can you do something about that, please? – barbara beeton May 31 '15 at 12:06
4

I have created another Greasemonkey (Firefox) / Tampermonkey (Chrome) script that use clipboard.js. When you hover a code section (<pre><code>) there is a small </> sign that appears on the top right corner of the script. You click it and voilà !

enter image description here

The code is available on this Gist.

How to use it :

  1. Install Greasemonkey (Firefox) / Tampermonkey (Chrome) for your browser.
  2. Read the code in the Gist (if you do not understand some code, do not install it) ! Click on the raw version of the gist.
  3. Install it.

Edit : I have created a second version. In the first version the button is not visible if the code is scrolled down. In the second version it stays visible ... and it has a different appearance (more \tex one ;)).

  • Does this work for code indented by 4 spaces as well? – Werner Dec 23 '15 at 20:50
  • I think yes : code indented by 4 spaces is wrapped in <pre><code>. – Kpym Dec 23 '15 at 20:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .