Bugzilla problem -- coexistence of GM event handler with theoriginal JS in the page

M

Matej Cepl

Hi,

I am a bug triager for RedHat's bugzilla (should I call myself "bug
greasemonkey @ RedHat" ;-)) and so I began to work on set of GM scripts to
tweak RH bugzilla for my own needs (note, that HTML on RH bug is invalid
and something absolutely horrible, so for example getElementById mostly
doesn't work at all -- somebody forgot to tell to BZ authors that id
should be unique in the page). Consider the script in the bottom of the
message. I've managed to make new button (with handler
changeOwnerHandler), but when trying to add another button setting
NEEDINFO for reporter of the bug (that's like almost 80% of bugs coming my
way) I got lost in dealing with the original Javascript functions.

For example, consider testing bug https://bugzilla.redhat.com/bugzilla/
show_bug.cgi?id=235018 -- how to invoke toggleNeedinfo so that it works.
When trying the solution below I got just error that document.forms
[1].newstatus doesn't exist.

Any ideas, what should I do?

Thanks a lot,

Matej

=================================================================
// version 0.1
// 2007-01-18
// Copyright (c) 2007, Matěj Cepl
// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
//
// ==UserScript==
// @name Bugzilla owner to CC-list
// @namespace http://www.ceplovi.cz/matej/progs/scripts
// @description add owner to CC list
// @include https://bugzilla.redhat.com/bugzilla/show_bug.cgi*
// ==/UserScript==

var GM_debug = true;

function getAssignedTo() {
var ATags = document.getElementsByTagName("a");
for (var i = 0; i < ATags.length; i++) {
if (ATags.innerHTML.match(/Assigned To/)) {
return(ATags[i+1].getAttribute("href").replace(/mailto:
(.*)/,"$1"));
}
}
}

function getCCList() {
var outL = [];
var member = "";
var SelectTag = document.getElementsByName("cc")[0];
for (var i = 0; i < SelectTag.length; i++) {
if(SelectTag.hasAttribute("value")) {
member = SelectTag.getAttribute("value");
member = member.replace(/\s*(.*)\s*/,"$1");
outL[outL.length] = member;
}
}
return(outL);
}

function isMemberOf(mbr,list) {
return(list.indexOf(mbr) !== -1);
}

function addNewButton(oldButton,newId,newLabel,handlerFunc) {
var newButton = oldButton.cloneNode(true);
newButton.id=newId;
newButton.setAttribute("value",newLabel);
if (GM_debug) {
newButton.addEventListener('click',handlerFunc,false);
} else {
newButton.addEventListener('click',handlerFunc,true);
}
var textNode = document.createTextNode("\u00A0");
oldButton.parentNode.insertBefore(textNode,oldButton);
oldButton.parentNode.insertBefore(newButton,textNode);
}

function changeOwnerHandler(evt) {
/** Take care that when changing assignment of the bug,
* current owner is added to CC list.
*/
if (!isMemberOf(owner,CCList)) {
var sel = document.getElementsByName("newcc")[0];
sel.setAttribute("value",owner);
}
if (GM_debug) {
evt.stopPropagation();
evt.preventDefault();
return false;
} else
return true;
}

function needinfoHandler(evt) {
/** set bug to NEEDINFO reporter.
*/
var sel = document.forms[1].elements.namedItem("newstatus");
var statesList = [];
var state = "";

for (var i=0; i < sel.length; i++) {
state = sel.getAttribute("value");
state = state.replace(/\s*(.*)\s*/,"$1");
statesList[statesList.length] = state;
}
var stateIndex = statesList.indexOf("needinfo");
var selectedState = sel.getElementsByTagName("option")[stateIndex];

document.getElementById("knob-changestatus").checked = true;
GM_log(document.forms[1].elements.namedItem("knob"));
GM_log(document.forms[1].elements.namedItem("knob").length);
document.forms[1].elements.namedItem("knob")[1].checked = true;
GM_log("here");
unsafeWindow.toggleNeedinfo(document.forms[1]);

selectedState.selected = true;
unsafeWindow.toggleNeedinfo(document.forms[1]);
var newOptions = document.getElementById("requestee_actor-16");
var newOptionList = newOptions.getElementsByTagName("option");
var newOptionReporter = {};
for (var i=0; i<newOptionList.length;i++) {
if (newOptionList.innerHTML.test(/Reporter/)) {
newOptionReporter = newOptionList;
}
}
GM_log(newOptionReporter);
if(newOptionReporter) {
newOptionReporter.selected=true;
}

if (GM_debug) {
evt.stopPropagation();
evt.preventDefault();
return false;
} else
return true;
}

var owner = getAssignedTo();
var CCList = getCCList();

// Vygeneruj seznam <input> elementů ve stránce
var IBList = [];
var IBRawList = document.getElementsByTagName("input");
for (var i=0;i<IBRawList.length;i++) {
if ((IBRawList.id.length >0) && (IBRawList.id=="button")) {
IBList = IBList.concat(IBRawList);
}
}

// Create a new SUBMIT button adding current owner of the bug to
// the CC list
var IBLast = IBList[IBList.length-1];
addNewButton(IBLast,"changeOwnerbtn","Change owner as
well",changeOwnerHandler);

// // Create another new SUBMIT button which sets NEEDINFO(reporter)
// var IBUnderTextBox = IBList[1];
// addNewButton(IBUnderTextBox,"needinforbtn","NEEDINFO
(reporter)",needinfoHandler);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top