Skip to content

Instantly share code, notes, and snippets.

@halirutan
Created October 9, 2014 21:29
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 halirutan/2217e4685ae82d754b6c to your computer and use it in GitHub Desktop.
Save halirutan/2217e4685ae82d754b6c to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Select Code Block Buttons
// @namespace stackoverflow
// @include *stackoverflow.com*
// @include *stackexchange.com*
// ==/UserScript==
(function ()
{
function with_jquery(f)
{
var script = document.createElement("script");
script.type = "text/javascript";
script.textContent = "(" + f.toString() + ")(jQuery)";
document.body.appendChild(script);
};
with_jquery(function ($)
{
addButtons();
function selectText(element)
{
var doc = document;
if (doc.body.createTextRange)
{
var range = doc.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection)
{
var selection = window.getSelection();
var range = doc.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
}
function addButtons()
{
$("pre").each(function (i, codeBlock)
{
var qContainer = $("<div></div>");
var id = "select-button-" + i;
$(codeBlock).replaceWith(qContainer);
qContainer.append(codeBlock);
qContainer.mouseenter(function ()
{
var qButton = $('<div style="position: absolute; opacity: 0; display: inline; cursor: pointer;' +
'background-color: #000; color: #fff; font-size: 12pt; padding: 3px;">' +
'Select</div>');
qButton.attr("id", id);
qContainer.append(qButton);
var left = $(codeBlock).offset().left + $(codeBlock).width() - qButton.width();
var top = $(codeBlock).offset().top;
qButton.css("left", left);
qButton.css("top", top);
qButton.click(function ()
{
selectText(codeBlock);
});
qButton.stop(true, true).animate({ opacity: '+=0.6' });
});
qContainer.mouseleave(function ()
{
$("#" + id).stop(true, true).animate({ opacity: '-=0.6' }, function () { $("#" + id).remove(); });
});
});
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment