33

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

7
  • How would this even be possible on a web site? Commented Jan 9, 2011 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
    Commented Jan 9, 2011 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
    Commented Jan 9, 2011 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 Mod
    Commented Jan 9, 2011 at 17:36
  • Yeah. Select the code for copying works just as well, and looks like it might be easier to implement.
    – Seamus
    Commented Jan 9, 2011 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!) Commented Jan 11, 2011 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
    Commented Jan 11, 2011 at 11:24

2 Answers 2

22

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

6
  • 3
    Please make it work also with Chrome.
    – lockstep
    Commented Feb 19, 2011 at 16:11
  • @lockstep: I will have a look on it Commented Feb 19, 2011 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
    Commented May 29, 2011 at 20:47
  • @lockstep: Does it work with the current version of Chrome?
    – Martin Scharrer Mod
    Commented May 18, 2012 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
    Commented Jan 19, 2013 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? Commented May 31, 2015 at 12:06
5

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 ;)).

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

You must log in to answer this question.

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