Skip to content

Instantly share code, notes, and snippets.

@Aran-Fey
Last active June 11, 2020 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aran-Fey/9b0855a1cec5a93a2157b3fd742785af to your computer and use it in GitHub Desktop.
Save Aran-Fey/9b0855a1cec5a93a2157b3fd742785af to your computer and use it in GitHub Desktop.
A userscript for StackExchange that adds a "copy to clipboard" button to every code block. Hovering over the bottom right corner of a code block makes the button appear.
// ==UserScript==
// @name StackExchange copy code to clipboard
// @description Adds a "copy to clipboard" button to each code block
// @version 1.0.10
// @author Paul Pinterits
// @include *://*.stackexchange.com/questions/*
// @include *://meta.serverfault.com/questions/*
// @include *://meta.stackoverflow.com/questions/*
// @include *://meta.superuser.com/questions/*
// @include *://serverfault.com/questions/*
// @include *://stackoverflow.com/questions/*
// @include *://superuser.com/questions/*
// @exclude *://*/questions/tagged/*
// @exclude *://*/questions/originals/*
// @require https://github.com/Aran-Fey/userscript-lib/raw/ca6999d1bac2494421b70286f74d7a9a9ba636e7/userscript_lib.js
// @require https://github.com/Aran-Fey/SE-userscript-lib/raw/bf77f40b25d7fa88a6c3f474390c858446154ec2/SE_userscript_lib.js
// @grant GM_setClipboard
// @updateURL https://gist.github.com/Aran-Fey/9b0855a1cec5a93a2157b3fd742785af/raw/SE_copy_code_to_clipboard.user.js
// @downloadURL https://gist.github.com/Aran-Fey/9b0855a1cec5a93a2157b3fd742785af/raw/SE_copy_code_to_clipboard.user.js
// ==/UserScript==
function copy_code(codeblock){
let code = codeblock.textContent;
GM_setClipboard(code, 'text');
}
function add_copy_code_buttons(post){
for (let codeblock of post.element.getElementsByTagName('PRE'))
add_overlay_button(codeblock, '⎘ Copy', copy_code.bind(null, codeblock));
}
page.transform_posts(add_copy_code_buttons, Rerun.AFTER_CHANGE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment