3

About

Highlighting and copying code from questions/answers is not as straightforward as one hopes it'd be. Throughout the years, there were (at least) a couple of userscripts/extensions to address this need. However, they are not maintained and the links are dead.

I have found this gist by kpym to be still functional, however it could've used a facelift.

This userscript (written off of the linked gist) adds a copy button to the top right corner of code blocks to copy the code from questions and answers.

Dark mode:

Light mode:

Installation

Click to install with a script manager. N.B. I have only tested this with Tampermonkey on Firefox.

Repository

Here is the source code for this userscript; source code repo

3

1 Answer 1

2

Following introduction of a copy button (with attribution) for code blocks on Stack Overflow, I've updated this userscript to offer a superior (imo), cross-site experience.

The advantages of having this script installed:

  • Works on all Stack Exchange websites (not just Stack Overflow)

  • Guaranteed plain-text attribution. The official SO feature often incorrectly guesses the programming language and comments out the attribution with the wrong syntax (see the relevant bug report), creating extra work. This script provides the attribution as plain text, letting you decide how to comment it out

  • Gives the right attribution (i.e. link to the post, author's name, date of retrieval, and license info) for all posts (regular Q&A's, community wikis, posts by deleted users, etc.)

  • The copy button only appears on hover (unlike SO's feature), saving screen space. It cleanly overrides (removes) the default Stack Overflow copy button

Click to install the updated version with a script manager. Note: I have only tested this with Tampermonkey on Firefox.

1
  • 1
    Thank you for this! Somewhat ironically, I've found this very useful as a way to get rid of the official space wasting implementation. Commented 2 days ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.