Tuesday, November 2, 2010

Script to nuke specific onclick() behavior in Rally tool.

When you're editing your user stories in Rally the most irritating part is that you can't open all your stories in new tabs by holding down the ctrl key and clicking all the story links. The following greasemonkey script enables you to do that:


// Makes Rally tool do what it is supposed to.
// 2009.03.17, Ross Rogers
// Copyright (c) 2005, Mark Pilgrmim
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need google's Chrome or
// Greasemonkey: https://addons.mozilla.org/en-US/firefox/addon/748
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Hello World", and click Uninstall.
// --------------------------------------------------------------------
//
// ==UserScript==
// @name Rally Onclick Nuke
// @namespace http://diveintogreasemonkey.org/download/
// @description Nukes the "onclick" attribute from user story links so you can
// CTRL click a link and have it open in a new tab. Also creates
// "accesskey" shortcuts for Save (alt+a), "edit_tags" (alt+t),
// and "apply" button (alt+a)
// @include https://*rally.sp*
// @include https://*slm/ar/edit.sp*
// ==/UserScript==

function remove_story_onclicks() {

console.log("running remove_story_onclicks\n");
var links = document.getElementsByTagName("a");
for (i = 0; i < links.length; i++) {
var node = links[i];
var link = node.getAttribute("href");
var className = node.getAttribute("class");
var is_edit_button = (className && className.indexOf("sprite-edit") > -1);
if (is_edit_button) {
var newHref = node.getAttribute("onclick").replace(/.*popup\('([^']*?)'.*/, "$1");
node.setAttribute("href",newHref);
}
if (link && link.indexOf("slm/detail/ar/") > -1 ||
is_edit_button ) {
if (node.getAttribute("onclick")) {
node.removeAttribute("onclick");
console.log("removed attribute onclick\n");
}
}
}
var buttons = document.getElementsByTagName("button");
for (i = 0; i < buttons.length; i++) {
var button = buttons[i];
console.log("Iterating through buttons\n");
console.log(button.innerHTML);
if (button.innerHTML.match(/^Save$/) ) {
button.setAttribute("accesskey","s");
console.log("setting Save button's accesskey to 's'\n");
};
if (button.innerHTML.match(/^Apply$/) ) {
button.setAttribute("accesskey","a");
console.log("setting Apply button's accesskey to 'a'\n");
};
};

var edit_tags_link = document.getElementById("edit_tags");
if (edit_tags_link) {
edit_tags_link.setAttribute("accesskey","t");
console.log("setting edit_tags-link's accesskey to 't'\n");
};


}


// override onclicks everywhere
var links = document.links;
for (i = 0; i < links.length; i++) {
var node = links[i];
console.log(node.onclick);
if (node.getAttribute("onclick")) {
console.log("wrappering onclick\n");
var original_onclick = node.onclick;
function wrapper_onclick(e ) {
var ret = original_onclick(e);
remove_story_onclicks();
return ret;
}
node.onclick = wrapper_onclick;
}
}

remove_story_onclicks();
function document_onclick() {
console.log("document_onclick() called\n");
remove_story_onclicks.apply(this,arguments);
}
document.onclick=document_onclick;

To install this, put it in a file named rally_onclick_nuke.user.js, then load it in chrome by dragging the file onto the browser. It should ask you at the bottom of the page if you want to install the extension, and you press yes.

No comments: