Shows a select-button on mouse over on code-blocks to select the whole block for easier copying.
Sorry to not provide a file for download but i am sure you will manage somehow, adjust includes as needed:
// ==UserScript==
// @name Select Code Block Buttons
// @namespace stackoverflow
// @include *stackoverflow.com*
// @version 2.0.0
// ==/UserScript==
// @ts-check
(function ()
{
const style = `
div > .gm_select_button {
position: absolute !important;
border: none !important;
outline: none !important;
cursor: pointer !important;
background-color: black !important;
color: white !important;
font-size: 12px !important;
padding: 3px !important;
top: 0 !important;
right: 0 !important;
transition: opacity linear 0.3s !important;
will-change: opacity !important;
opacity: 0;
}
div:hover > .gm_select_button {
opacity: 0.5 !important;
}
`;
const styleNode = document.createElement("style");
styleNode.type = "text/css";
styleNode.innerHTML = style;
document.head.appendChild(styleNode);
[...document.querySelectorAll("pre")].forEach((codeBlock, i) =>
{
const container = document.createElement("div");
container.style.position = "relative";
codeBlock.parentElement.replaceChild(container, codeBlock);
container.appendChild(codeBlock);
const button = document.createElement("button");
button.classList.add("gm_select_button");
button.textContent = "Select";
container.appendChild(button);
button.addEventListener("click", () => selectText(codeBlock));
});
function selectText(element)
{
if (document.body.createTextRange)
{
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection)
{
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
}
})();
Edits:
- Added the jQuery frame from the SE keyboard shortcuts script.
- Changed text selection code to be a bit more browser-independent.
- Buttons are now created on mouse-over, making sure that the placement is correct as the layout may change (e.g. by expanding a comment thread).
- Removed cancer.
- Mark the button style properties as
!important
because they get overridden otherwise.
(Requires a fairly current browser.)
apps.Slack.com