public
Last active

A simple UserScript that adds a 'trello' link to meta.* StackExchange posts to allow the user to rapidly create a trello card from the post. The purpose of this is to make SE sites even more useful as a bug-tracking / community management tool. For a brief screencast, see: http://youtu.be/AUJ27ISlTVI

  • Download Gist
stacktrello.user.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
// ==UserScript==
// @name StackTrello
// @author Benjamin Collins <ben.collins@acm.org>
// @namespace https://gist.github.com/aggieben
// @downloadURL https://gist.github.com/5811685/raw/stacktrello.user.js
// @updateURL https://gist.github.com/5811685/raw/stacktrello.user.js
// @version 0.0.3
// @downloadURL ttps://gist.github.com/aggieben/5811685/raw/stacktrello.user.js
// @updateURL ttps://gist.github.com/aggieben/5811685/raw/stacktrello.user.js
// @description Script to render a link that will create a new trello card from a StackExchange site post
// @match *://*.stackoverflow.com/q*
// @match *://*.stackexchange.com/q*
// @match *://*.superuser.com/q*
// @match *://*.serverfault.com/q*
// @run-at document-end
// @copyright 2013+, Benjamin Collins
// @require http://code.jquery.com/jquery.min.js
// @require https://github.com/alexei/sprintf.js/raw/master/src/sprintf.min.js
// @require https://api.trello.com/1/client.js?key=e9002cf6caab2a10867d22384966ab42
// @require https://github.com/domchristie/to-markdown/raw/master/src/to-markdown.js
// ==/UserScript==
// Settings for users to modify
 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
var log_levels = { trace: 5, debug: 4, info: 3, warn: 2, err: 1, off: 0 };
var log_level = log_levels.warn;
 
var se_client_id = 1685;
var se_api_key = 'lHi*7B5IMDmzKmssD34nkQ((';
var se_user_token = GM_getValue('se_user_token', null);
 
var trello_api_key = "e9002cf6caab2a10867d22384966ab42";
var trello_api_baseUrl = 'https://api.trello.com/1';
var trello_user_token = GM_getValue('trello_user_token', null);
var trello_cache = {};
 
var trelloTipHtml = ' \
<div class="trellopopup share-tip"> \
<p class="messages"></p> \
<p>Board: <select class="board"></select></p> \
<p>List: <select class="list"></select></p> \
<p>Labels: <select class="labels"></select></p> \
<a class="close-share-tip" style="float: left;">close</a> \
<button style="float: right;">create card</button> \
</div> \
';
 
var trelloLabelColors = {
"blue": "#5F86C9",
"green": "#5FC984",
"orange": "#C9AD5F",
"purple": "#B48BD6",
"red": "#D68B8B",
"yellow": "#D6D58B"
};
 
GM_log(sprintf("loaded StackTrello v%s with jquery %s",
GM_info.script.version,$.fn.jquery));
 
function trace(msg) {if (log_level > log_levels.trace) GM_log(sprintf('trace: %s', msg));}
function debug(msg) {if (log_level > log_levels.debug) GM_log(sprintf('debug: %s', msg));}
function info(msg) {if (log_level > log_levels.info) GM_log(sprintf('info: %s', msg));}
function warn(msg) {if (log_level > log_levels.warn) GM_log(sprintf('warn: %s', msg));}
function err(msg) {if (log_level > log_levels.err) GM_log(sprintf('err: %s', msg));}
 
function trello_get(path, success, onerror) {
var url = null;
if (/\?/.test(path)) {
url = sprintf('%s/%s&key=%s&token=%s',
trello_api_baseUrl, path, trello_api_key, trello_user_token);
} else {
url = sprintf('%s/%s?key=%s&token=%s',
trello_api_baseUrl, path, trello_api_key, trello_user_token);
}
 
debug('requesting: ' + url);
return {
url: url,
method: 'GET',
headers: { "Accept": "application/json" },
onload: success,
onerror: onerror || function(response) { GM_log(response); }
};
}
 
function trello_post(path, data, success, error) {
var url = null;
if (/\?/.test(path)) {
url = sprintf('%s/%s&key=%s&token=%s',
trello_api_baseUrl, path, trello_api_key, trello_user_token);
} else {
url = sprintf('%s/%s?key=%s&token=%s',
trello_api_baseUrl, path, trello_api_key, trello_user_token);
}
 
debug(sprintf('posting %s with data: %s', url, JSON.stringify(data)));
return {
url: url,
method: 'POST',
data: JSON.stringify(data),
headers: { "Accept": "application/json", "Content-Type": "application/json" },
onload: success,
onerror: error || function(response) { GM_log(response); }
};
}
 
function trelloAuthorize() {
Trello.authorize({
type: 'popup',
name: GM_info.script.name,
scope: { read: true, write: true },
success: function() {
if (Trello.token()) {
trello_user_token = Trello.token();
GM_setValue('trello_user_token', trello_user_token);
debug('authenticated with token: ' + trello_user_token);
}
}
});
}
 
function updateBoardOptions(boards) {
trace('updating boards');
$board = $('select.board');
$board.empty();
for ( var i in boards ) {
$board.append(sprintf('<option value="%s">%s</option>', boards[i].id, boards[i].name));
}
 
GM_xmlhttpRequest(
trello_get(
sprintf('boards/%s/lists?filter=open', boards[0].id),
function(response) {
updateListOptions(JSON.parse(response.responseText));
updateLabelOptions(boards[0].labelNames)
}));
}
 
function updateListOptions(lists) {
trace('updating lists');
$list = $('select.list');
$list.empty();
for ( var i in lists) {
$list.append(sprintf('<option value="%s">%s</option>', lists[i].id, lists[i].name));
}
}
 
function updateLabelOptions(labels) {
trace('updating labels');
$list = $('select.labels');
$list.empty();
$list.append('<option value="">select one</option>');
for (var l in labels) {
$list.append(
sprintf('<option style="background-color: %s;" value="%s">%s</option>',
trelloLabelColors[l], l, labels[l] || sprintf('-%s-',l)));
}
}
 
function loadTrelloBoards() {
GM_xmlhttpRequest(
trello_get(
'members/me/boards?filter=open',
function(response) {
if (response.status != 200) {
if (/invalid token/i.test(response.responseText) || /unauthorized/i.test(response.statusText)) {
warn('invalid token');
$('div.trellopopup p.messages').html('An error has occurred. Please try again');
trello_user_token = null;
GM_setValue('trello_user_token', null);
Trello.deauthorize();
trelloAuthorize();
return;
}
}
var json = JSON.parse(response.responseText);
updateBoardOptions(json);
})
);
}
 
function loadTrelloLists(boardId) {
GM_xmlhttpRequest(
trello_get(
sprintf('boards/%s/lists?filter=open', boardId),
function(response) {
var lists = JSON.parse(response.responseText);
updateListOptions(lists);
}));
}
 
function loadTrelloBoard(boardId) {
GM_xmlhttpRequest(
trello_get(
sprintf('boards/%s', boardId),
function(response) {
var board = JSON.parse(response.responseText)
updateLabelOptions(board.labelNames)
loadTrelloLists(boardId);
}));
}
 
function createTrelloCard(options) {
GM_xmlhttpRequest(
trello_post(
'cards',
{ name: options.name, idList: options.listId, labels: options.labels, due: null, desc: options.desc },
options.success));
}
 
function showTrelloTip($link) {
if (!trello_user_token) {
trelloAuthorize();
} else {
debug('using token: ' + trello_user_token);
}
 
loadTrelloBoards();
 
var $div = $(trelloTipHtml);
var p = $link.parents('.post-menu');
$div.appendTo(p)
.fadeIn('fast')
.find('.close-share-tip').click(function() {
$div.fadeOut('fast', function() {
$(this).trigger('removing').remove();
});
});
 
$('select.board').on('change', function() {
var id = $(this).val();
loadTrelloBoard(id);
});
 
$('div.trellopopup button').click(function(event) {
var idattr = null;
$parent = $(this).parents('div.question, div.answer');
if ($parent.hasClass('question')) idattr = 'data-questionid';
else if ($parent.hasClass('answer')) idattr = 'data.answerid';
else warn("unable to get post id");
 
info("create the card for post " + $parent.attr(idattr));
var cardTitle = $('#question-header a').text();
var cardText = sprintf('[Meta Post](https://%s/q/%s)\n\n---\n\n%s',
window.location.host,
$parent.attr(idattr),
$parent.find('div.post-text').html());
createTrelloCard({
name: cardTitle,
desc: toMarkdown(cardText.trim()),
listId: $('select.list').val(),
labels: [$('select.labels').val()],
success: function(data) {
$div.fadeOut('fast', function() {
$(this).trigger('removing').remove();
});
}
});
});
}
 
// main script starts here
//////////////////////////////////////////////////////////////////
$('div.post-menu').append(function(index) {
var $parent = $(this).parents('div.question, div.answer');
var result = ['<span class="lsep">|</span>'];
 
if ($parent.hasClass('question')) {
result.push('<a href="#" title="Create a Trello card from this post" class="stacktrello" data-postid="' + $parent.attr('data-questionid') + '">trello</a>');
} else if ($parent.hasClass('answer')) {
result.push('<a href="#" title="Create a Trello card from this post" class="stacktrello" data-postid="' + $parent.attr('data-answerid') + '">trello</a>');
}
 
return result.join("");
});
 
$('a.stacktrello').on('click', function(event) {
$this = $(event.target);
showTrelloTip($this);
 
return false;
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.