Thank you for this script. It was basically the thing I was looking for when I asked for a [Second 'link' button below posts which provides a comment style link in the form post title](http://meta.stackoverflow.com/q/126544/156875https://meta.stackoverflow.com/q/126544/156875).
// ==UserScript==
// @name Copy Question Link Markdown (modified)
// @author Nathan Osman
// @author Martin Scharrer
// @namespace http://quickmediasolutions.com
// @description Adds a button for copying the markdown of a post's URL to the clipboard
// @include http://stackoverflow.com/*
// @include httphttps://meta.stackoverflow.com/*
// @include http://superuser.com/*
// @include http://serverfault.com/*
// @include http://meta.superuser.com/*
// @include http://meta.serverfault.com/*
// @include http://stackapps.com/*
// @include http://askubuntu.com/*
// @include http://*.stackexchange.com/*
// ==/UserScript==
/* Modified by Martin Scharrer 14th April 2012 to support links to answers and to use short form URLs.
*
* Changelog:
* o Added support for answer posts.
* - The pop-up window now appears beside the clicked 'copy' button, not at the very first one.
* - The included URL will point to the post the button belongs to, not always to the question.
* o The short version of the URL is now used to minimize the number of characters.
* o Changed "id='copy-button'" to "class='copy-button'" to allow multiple buttons.
* Before all posts got the button, which incorrectly had all the same id.
* o The object of the 'copy' button is now passed along with the click, not determined by its id.
* */
// Here I borrow a couple functions I wrote for another
// UserScript that makes it easy to provide functions
// with complete access to the page.
function EmbedFunctionOnPageAndExecute(function_contents)
{
var exec_script = document.createElement('script');
exec_script.type = 'text/javascript';
exec_script.textContent = "(" + function_contents.toString() + ")()";
document.getElementsByTagName('head')[0].appendChild(exec_script);
}
// ...the other one
function EmbedFunctionOnPage(function_name, function_contents)
{
var exec_script = document.createElement('script');
exec_script.type = 'text/javascript';
exec_script.textContent = function_contents.toString().replace(/function ?/, 'function ' + function_name);
document.getElementsByTagName('head')[0].appendChild(exec_script);
}
// The code to execute when the ZeroClipboard lib loads
EmbedFunctionOnPage('DoCopy', function(button) {
// Get ids required to build short URL to the post
var questionid = location.path.match(/\/questions\/(\d+)\/.*/)[1];
var shortquestionpath = $("#link-post-" + questionid).attr("href");
var userid = shortquestionpath.substr( questionid.length + 4 ); // "/q/<questionid>/<userid>"
var answerid = $(button).parents("div.answer").log("answer").attr("data-answerid");
var shorturl;
if (answerid) {
shorturl = "http://" + location.host + "/a/" + answerid + "/" + userid;
}
else {
shorturl = "http://" + location.host + shortquestionpath;
}
// Determine the content of the DIV
var content = '<b>Copy the following:</b><br /><br /><textarea id="copy-content" style="width: 440px; height: 80px;">[' +
$('#question-header .question-hyperlink').text() + '](' + shorturl + ')</textarea>';
$('body').append('<div id="copy-popup" class="popup" style="width: 450px; position: absolute; display: none;">' +
'<div class="popup-close"><a title="close this popup">×</a></div>' + content + '</div>');
// Assign the close action to the 'x'
$('#copy-popup .popup-close').click(function() { $('#copy-popup').fadeOut(300, function() { $('#copy-popup').remove(); }); });
// Position the dialog
var pos = $(button).position();
var height = $(button).height();
$('#copy-popup').css('left', pos.left);
$('#copy-popup').css('top', pos.top + height + 3);
$('#copy-popup').fadeIn(500, function() {
// Select the contents
var textarea = document.getElementById('copy-content');
textarea.focus();
textarea.select();
});
});
// The code that gets executed right away.
EmbedFunctionOnPageAndExecute(function() {
jQuery.fn.log = function (msg) {
console.log("%s: %o", msg, this);
return this;
};
// Embed the copy item
$('.post-menu').append("<span class='lsep'>|</span><a href='javascript:void(0)' onclick='DoCopy(this)' class='copy-button'>copy</a>");
});