Last active
June 11, 2020 21:33
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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