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

Discussion in 'Javascript' started by Matej Cepl, Apr 4, 2007.

  1. Matej Cepl

    Matej Cepl Guest

    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);
    Matej Cepl, Apr 4, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S2V2aW4gQnVydG9u?=

    Visual Studio 2003 and 2005 coexistence.

    =?Utf-8?B?S2V2aW4gQnVydG9u?=, Sep 4, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    473
    Juan T. Llibre
    Sep 4, 2006
  2. Replies:
    1
    Views:
    695
    Damien
    Feb 22, 2007
  3. ASP ASP.NET Coexistence

    , Jul 25, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    418
    Cowboy \(Gregory A. Beamer\)
    Jul 26, 2008
  4. Max2006

    ASP.NET Winforms and MVC coexistence

    Max2006, Apr 1, 2009, in forum: ASP .Net
    Replies:
    4
    Views:
    589
    Max2006
    Apr 9, 2009
  5. Michael Schuerig

    Debian: coexistence of debs and gems?

    Michael Schuerig, May 4, 2005, in forum: Ruby
    Replies:
    4
    Views:
    128
    Michael Schuerig
    May 5, 2005
Loading...

Share This Page