Skip to content

Instantly share code, notes, and snippets.

@slhck
Last active December 15, 2015 09:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save slhck/5236026 to your computer and use it in GitHub Desktop.
Copy User Links
// ==UserScript==
// @name Copy User Links
// @author Werner Robitza
// @namespace
// @description Adds a button for copying superping-ready user names of close voters
// @include http://stackoverflow.com/*
// @include http://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==
// This is based on Copy Question Link Markdown
// http://stackapps.com/questions/2150/
// 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('DoCopyLinks', function() {
var output = "";
$(".question-status h2 a[href^='/users/']").each(function(){ output += "@@" + $(this).attr("href").match(/\/.*\/(\d+)\/.*/)[1] + "@" + document.location.hostname + " "; });
// Determine the content of the DIV
var links_content = '<b>Copy the following:</b><br /><br /><textarea id="copy-links-content" style="width: 440px; height: 80px;">' +
output + '</textarea>';
$('body').append('<div id="copy-links-popup" class="popup" style="width: 450px; position: absolute; display: none;">' + '<div class="popup-close"><a title="close this popup">×</a></div>' + links_content + '</div>');
// Assign the close action to the 'x'
$('#copy-links-popup .popup-links-close').click(function() { $('#copy-links-popup').fadeOut(300, function() { $('#copy-links-popup').remove(); }); });
// Position the dialog
var pos = $('#copy-links-button').position();
var height = $('#copy-links-button').height();
$('#copy-links-popup').css('left', pos.left);
$('#copy-links-popup').css('top', pos.top + height + 3);
$('#copy-links-popup').fadeIn(500, function() {
// Select the contents
var textarea = document.getElementById('copy-links-content');
textarea.focus();
textarea.select();
});
});
// The code that gets executed right away.
EmbedFunctionOnPageAndExecute(function() {
if ($(".question-status h2 a").length > 0) {
// Embed the copy item
$('.question .post-menu').append("<span class='lsep'>|</span><a href='javascript:void(0)' onclick='DoCopyLinks()' id='copy-links-button'>@@</a>");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment